如何从 MySQL 列中获取前 N 个字符


使用 SUBSTRING() 从 MySQL 列获取前 N 个字符。我们首先创建一个表 -

mysql>create table DemoTable
(
   Information text
);
Query OK, 0 rows affected (2.63 sec)

使用插入命令在表中插入记录 -

mysql>insert into DemoTable values('MySQL is a structured query language');
Query OK, 1 row affected (0.13 sec)

以下是使用 select 语句从表中显示所有记录的查询 -

mysql>select *from DemoTable;

这会产生以下输出 -

+--------------------------------------+
| Information                          |
+--------------------------------------+
| MySQL is a structured query language |
+--------------------------------------+
1 row in set (0.00 sec)

这是从列中获取前 N 个字符的查询。在本例中,我们选择了前 10 个字符 -

mysql>select substring(Information,1,10) from DemoTable;

这会产生以下输出 -

+-----------------------------+
| substring(Information,1,10) |
+-----------------------------+
| MySQL is a                  |
+-----------------------------+
1 row in set (0.00 sec)

更新于: 2019 年 7 月 30 日

859 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.