mysql:检查数据库表中是否存在数组值

时间:2014-07-26 06:32:13

标签: mysql arrays

我有一个PHP数组(0,1,2,3,4,5),我想在我的数据库表中查看哪些是

我该怎么做:

SELECT num FROM (0,1,2,3,4,5) AS num WHERE num NOT IN (SELECT id FROM sometable);

我正在询问正确的SQL synthax。

1 个答案:

答案 0 :(得分:1)

create table sometable (num int not null);
insert into sometable values (1),(1),(4);

解决方案:

create temporary table tmp (num int not null);
insert into tmp values (0),(1),(2),(3),(4),(5);
select t.num from tmp t left join sometable s on t.num=s.num where s.num is null;

或者

select t.num from tmp t where t.num not in (select num from sometable);

输出:

+-----+
| num |
+-----+
|   2 |
|   3 |
|   5 |
+-----+