要返回前n个字母,请使用LEFT()
函数。以下是语法-
select left(yourColumnName,yourValue) from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CourseTitle text -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(CourseTitle) values('Java with Spring and Hibernate framework'); mysql> insert into DemoTable(CourseTitle) values('Python Web Development');
使用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)