MySQL 中适用于 IN 运算符的逗号分隔参数?
对命令分隔的参数使用 FIND_IN_SET()。我们先创建一个表格 -
mysql> create table DemoTable604 (Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,Title varchar(100)); Query OK, 0 rows affected (0.51 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable604(Title) values('MySQL'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable604(Title) values('C++'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable604(Title) values('MongoDB'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable604(Title) values('Java'); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表格中的所有记录 -
mysql> select *from DemoTable604;
这将产生以下输出 -
+----+---------+ | Id | Title | +----+---------+ | 1 | MySQL | | 2 | C++ | | 3 | MongoDB | | 4 | Java | +----+---------+ 4 rows in set (0.00 sec)
以下是逗号分隔参数的查询 -
mysql> select *from DemoTable604 where find_in_set(Title,'C++,MongoDB');
这将产生以下输出 -
+----+---------+ | Id | Title | +----+---------+ | 2 | C++ | | 3 | MongoDB | +----+---------+ 2 rows in set (0.00 sec)
广告