您可以使用substring()
MySQL中的字段来获取字符串的一部分。以下是语法-
select substring(yourColumnName,yourStartingIndex,yourEndingIndex) from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Title longtext );
使用插入命令在表中插入记录-
mysql> insert into DemoTable(Title) values('MySQL is a relational database management system'); mysql> insert into DemoTable(Title) values('MongoDB is a popular No SQL database');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+----+--------------------------------------------------+ | Id | Title | +----+--------------------------------------------------+ | 1 | MySQL is a relational database management system | | 2 | MongoDB is a popular No SQL database | +----+--------------------------------------------------+ 2 rows in set (0.00 sec)
以下是substring()
对字段使用方法的查询-
mysql> select substring(Title,1,21) from DemoTable;
这将产生以下输出-
+-----------------------+ | substring(Title,1,21) | +-----------------------+ | MySQL is a relational | | MongoDB is a popular | +-----------------------+ 2 rows in set (0.00 sec)