2013-12-10 43 views
0

我有一個表一中,我有以下5個記錄Mysql的Select語句查找記錄表中沒有

ID 
--- 
1 
2 
3 
4 
5 

我需要一個查詢將返回在表中找不到記錄,東西沿線作者:

select ID from A where ID in (1,2,3,4,5,6) 

我希望看到ID 6返回,因爲在表中找不到ID 6。

ID 
--- 
6 

回答

0

做到這一點,最簡單的方法是存儲你想在一個表(可能是一個臨時表)來查找值,然後左連接這些表:

create temporary table temp_values (
    val int not null, 
    index idx_val(val) 
); 
insert into temp_values (val) values (1), (2), (3), (4), (5), (6); 

select 
    t.val 
from 
    temp_values as t 
    left join yourTable as a on t.val = a.id 
where 
    a.id is null; 

希望這有助於。