如何使用MySQL查询从列值中仅删除第一个单词?
要仅从列值中删除第一个单词,请使用substring()
。以下是语法-
select substring(yourColumnName,locate(' ',yourColumnName)+1) AS anyAliasName from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( Title text );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Java in Depth'); mysql> insert into DemoTable values('C++ is an object oriented programming language'); mysql> insert into DemoTable values('MySQL is a relational database'); mysql> insert into DemoTable values('Python with data structure');
使用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)