我能否使用 MySQL 查询在包含以逗号分隔的记录的 MySQL 列中搜索特定数字?
是的,可以使用 MySQL FIND_IN_SET() 搜索特定数字。我们首先创建一个表 -
mysql> create table DemoTable ( ListOfNumbers varchar(100) ); Query OK, 0 rows affected (1.24 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values('784,746,894,344'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('456,322,333,456'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('654,785,678,456'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values('123,676,847,785'); Query OK, 1 row affected (0.34 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
这会生成以下输出 -
+-----------------+ | ListOfNumbers | +-----------------+ | 784,746,894,344 | | 456,322,333,456 | | 654,785,678,456 | | 123,676,847,785 | +-----------------+ 4 rows in set (0.00 sec)
以下是用于搜索记录中特定数字的查询 -
mysql> select *from DemoTable where find_in_set('456',ListOfNumbers);
这会生成以下输出 -
+-----------------+ | ListOfNumbers | +-----------------+ | 456,322,333,456 | | 654,785,678,456 | +-----------------+ 2 rows in set (0.28 sec)
广告