为了删除空格后的所有内容,您需要使用SUBSTRING_INDEX()。
语法如下select substring_index(yourColumnName,' ',1) as anyAliasName from yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下mysql> create table deleteAfterSpaceDemo
-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> StudentName varchar(100)
-> );
使用insert命令在表中插入一些记录。
查询如下mysql> insert into deleteAfterSpaceDemo(StudentName) values('John Smith');
mysql> insert into deleteAfterSpaceDemo(StudentName) values('Adam Smith');
mysql> insert into deleteAfterSpaceDemo(StudentName) values('Carol Taylor');
mysql> insert into deleteAfterSpaceDemo(StudentName) values('Chris Brown');
mysql> insert into deleteAfterSpaceDemo(StudentName) values('David Miller');
使用select语句显示表中的所有记录。
查询如下mysql> select *from deleteAfterSpaceDemo;
以下是输出+----+--------------+
| Id | StudentName |
+----+--------------+
| 1 | John Smith |
| 2 | Adam Smith |
| 3 | Carol Taylor |
| 4 | Chris Brown |
| 5 | David Miller |
+----+--------------+
5 rows in set (0.00 sec)
这是删除空格后所有内容的查询mysql> select substring_index(StudentName,' ',1) as deleteAllAfterSpace from deleteAfterSpaceDemo;
以下是输出+---------------------+
| deleteAllAfterSpace |
+---------------------+
| John |
| Adam |
| Carol |
| Chris |
| David |
+---------------------+
5 rows in set (0.04 sec)