2014-07-26 10 views
0

숫자가 (0,1,2,3,4,5) 인 PHP 배열이 있는데 그 중 어떤 것이 0인지 확인하고 싶습니다. 이 아닌이 있습니다. DB 테이블. 내가 올바른 SQL의 synthax에 대해 부탁 해요mysql : 배열의 값이 DB 테이블에 있는지 확인하십시오.

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

:

어떻게 그런 짓을 할 수 있습니다.

+1

들으, 그 나를 위해 일하고있어! –

답변

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 | 
+-----+ 
관련 문제