在 MySQL 查询中匹配数组的元素
我们首先创建一个表 table −
mysql> create table DemoTable1523 -> ( -> Id int, -> Value int -> ); Query OK, 0 rows affected (0.76 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1523 values(1,56); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1523 values(2,78); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1523 values(1,34); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1523 values(2,45); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1523 values(1,99); Query OK, 1 row affected (0.10 sec)
使用 select 语句从表中显示所有记录 −
mysql> select * from DemoTable1523;
这将产生以下输出 −
+------+-------+ | Id | Value | +------+-------+ | 1 | 56 | | 2 | 78 | | 1 | 34 | | 2 | 45 | | 1 | 99 | +------+-------+ 5 rows in set (0.00 sec)
以下是匹配数组元素的查询。在这里,我们使用匹配值 34、99 和 56 选中 id −
mysql> select Id from DemoTable1523 -> where Value IN ('34','99','56') -> group by Id -> having count(*)=3;
这将产生以下输出 −
+------+ | Id | +------+ | 1 | +------+ 1 row in set (0.00 sec)
广告