在 MySQL 中返回某列的前 n 个字母
mysqlmysqli database
要返回前 n 个字母,请使用 LEFT() 函数。语法如下 −
select left(yourColumnName,yourValue) from yourTableName;
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CourseTitle text -> ); Query OK, 0 rows affected (0.65 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(CourseTitle) values('Java with Spring and Hibernate framework'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable(CourseTitle) values('Python Web Development'); Query OK, 1 row affected (0.22 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+----+------------------------------------------+ | Id | CourseTitle | +----+------------------------------------------+ | 1 | Java with Spring and Hibernate framework | | 2 | Python Web Development | +----+------------------------------------------+ 2 rows in set (0.00 sec)
以下查询返回某列的前 n 个字母 −
mysql> select LEFT(CourseTitle,20) from DemoTable;
输出
+----------------------+ | left(CourseTitle,20) | +----------------------+ | Java with Spring and | | Python Web Developme | +----------------------+ 2 rows in set (0.00 sec)