技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

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

mysqlmysqli database

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

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

使用 insert 命令在表中插入记录 −

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)

相关文章