2014-01-14 68 views
0

我認爲我已經做了儘可能多的我知道的這一步MySQL從條件得到價值

難以將下面的語句轉換爲查詢。從house_info

其中house_info.house_code在family_info


house_code不存在


選擇house_code是主鍵和在house_info表是唯一的。 然而,house_codefamily_info不是唯一的也不是主鍵(同一處house_code倍數存在)

我試圖讓house_code值並不在family_info相比存在house_info

前)house_code在house_info (1,2,3,4,5)和family_info house_code(1,1,2,2,3-)

結果:4,5

我曾嘗試使用內部連接,如果不存在和許多它似乎其他查詢,但不工作,我想

香港專業教育學院還試圖

SELECT a.house_code FROM house_info a, family_info b WHERE a.house_code!=b.house_code; 

回答

1
SELECT house_code FROM house_info 
WHERE house_code NOT IN (SELECT house_code FROM family_info) 

按照你例如:

SELECT house_code FROM house_info 

==>(1,2,3,4,5)= A

(SELECT house_code FROM family_info) 

==>(1,1,2 ,2,3)= B

A NOT IN B 

==>(4,5)

1

一種可能的方式去那裏

select house_code from house_info 
where house_info.house_code not in (select house_code from family_info) 

not in的意思是」值不是一部分這組值「,嵌套選擇提供了一組值。

1

使用單向連接的簡單方法:

select 
    h.house_code 
from 
    house_info as h 
    left join family_info as f on h.house_code = f.house_code 
where 
    f.house_code is null