在 MySQL 中从逗号分隔的字符串中删除特定单词
我们首先创建一个表 -
mysql> create table DemoTable836(FirstName SET('John','Chris','Adam')); Query OK, 0 rows affected (0.60 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable836 values('John,Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable836 values('John,Chris,Adam'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable836 values('Chris,Adam'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable836 values('John,Adam'); Query OK, 1 row affected (0.37 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable836;
这将产生以下输出 -
+-----------------+ | FirstName | +-----------------+ | John,Chris | | John,Chris,Adam | | Chris,Adam | | John,Adam | +-----------------+ 4 rows in set (0.00 sec)
以下是如何删除逗号分隔字符串中的特定单词的查询 -
mysql> update DemoTable836 set FirstName = FirstName &~ (1 << FIND_IN_SET('Chris', FirstName) - 1); Query OK, 3 rows affected (0.21 sec) Rows matched: 4 Changed: 3 Warnings: 0
让我们再次检查表记录 -
mysql> select *from DemoTable836;
这将产生以下输出 -
+-----------+ | FirstName | +-----------+ | John | | John,Adam | | Adam | | John,Adam | +-----------+ 4 rows in set (0.00 sec)
广告信息