如何通过 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)

更新日期: 2019-09-25

576 次浏览

开启您的 职业生涯

完成课程获得认证

开始
广告