在 MySQL 中查找包含由逗号分隔的多个 id 的特定行?
要查找一行,请使用 FIND_IN_SET()。让我们首先创建一个表 -
mysql> create table DemoTable -> ( -> ListOfIds varchar(200) -> ); Query OK, 0 rows affected (0.72 sec)
使用 insert 命令向表中插入一些记录 -
mysql> insert into DemoTable values('100,2093,678,686'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('0595,9585,4885,95959'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('0059954,95986884,9059596,9005'); Query OK, 1 row affected (0.20 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
输出
+-------------------------------+ | ListOfIds | +-------------------------------+ | 100,2093,678,686 | | 0595,9585,4885,95959 | | 0059954,95986884,9059596,9005 | +-------------------------------+ 3 rows in set (0.00 sec)
以下是在 MySQL 中查找包含由逗号分隔的多个 id 的特定行的查询 -
mysql> select *from DemoTable where FIND_IN_SET('9005', ListOfIds);
输出
+-------------------------------+ | ListOfIds | +-------------------------------+ | 0059954,95986884,9059596,9005 | +-------------------------------+ 1 row in set (0.00 sec)
广告