使用MySQL删除逗号分隔的字符串中的特定单词
让我们首先创建一个表-
mysql> create table DemoTable836(FirstName SET('John','Chris','Adam'));
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable836 values('John,Chris'); mysql> insert into DemoTable836 values('John,Chris,Adam'); mysql> insert into DemoTable836 values('Chris,Adam'); mysql> insert into DemoTable836 values('John,Adam');
使用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); 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)