如何用MySQL查询仅删除列值中的第一个单词?
使用substring()删除列值中的第一个单词。以下是语法-
select substring(yourColumnName,locate(' ',yourColumnName)+1) AS anyAliasName from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable
(
Title text
);
Query OK, 0 rows affected (0.50 sec)
使用insert命令在表中插入一些记录-
mysql> insert into DemoTable values('Java in Depth');
Query OK, 1 row affected (0.49 sec)
mysql> insert into DemoTable values('C++ is an object oriented programming language');
Query OK, 1 row affected (0.47 sec)
mysql> insert into DemoTable values('MySQL is a relational database');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Python with data structure');
Query OK, 1 row affected (0.24 sec)
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------------------------------------------+
| Title |
+------------------------------------------------+
| Java in Depth |
| C++ is an object oriented programming language |
| MySQL is a relational database |
| Python with data structure |
+------------------------------------------------+
4 rows in set (0.00 sec)
以下是从列值中删除第一个单词的查询-
mysql> select substring(Title,locate(' ',Title)+1) AS RemoveFirstWord from DemoTable;
这将产生以下输出-
+--------------------------------------------+
| RemoveFirstWord |
+--------------------------------------------+
| in Depth |
| is an object oriented programming language |
| is a relational database |
| with data structure |
+--------------------------------------------+
4 rows in set (0.00 sec)
阅读更多:MySQL 教程
极客教程