将字符串“Learn With Ease”拆分并返回最后一个单词的 MySQL 查询?
对于此,可以使用 SUBSTRING_INDEX()。我们先创建一个表 −
mysql> create table DemoTable ( Words TEXT ); Query OK, 0 rows affected (1.62 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('Learn With Ease'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Intro To MySQL'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('Deep Dive Using Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('C++ In Depth'); Query OK, 1 row affected (0.16 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
这将生成以下输出 −
+-----------------------+ | Words | +-----------------------+ | Learn With Ease | | Intro To MySQL | | Deep Dive Using Java | | C++ In Depth | +-----------------------+ 4 rows in set (0.00 sec)
以下是将字符串“Learn With Ease”拆分并返回最后一个单词的查询 −
mysql> update DemoTable set Words=substring_index(Words,' ',-1); Query OK, 4 rows affected (0.28 sec) Rows matched : 4 Changed : 4 Warnings : 0
我们再次检查表记录 −
mysql> select *from DemoTable;
这将生成以下输出 −
+----------+ | Words | +----------+ | Ease | | MySQL | | Java | | Depth | +----------+ 4 rows in set (0.00 sec)
广告