2013-07-02 33 views
0

所以我有兩個表。一個叫做superID,它有一列「superID BIGINT」,另一個叫做mainID,它有一個名爲「subid BIGINT」的列。我知道mainIDS是superID的一個子集。如何查看僅在superID但不在mainID中的行。mysql獲得2個結果集的差異

這裏是我的解決方案的嘗試:

SELECT * FROM atlas_comparables.superIDs WHERE NOT EXISTS 
(SELECT * FROM atlas_comparables.mainIDs); 

然而,這回我一個空集。任何幫助?

回答

2

試試這個

SELECT * FROM atlas_comparables.superIDs WHERE the_id_column NOT IN 
    (SELECT the_idcolumn_ofcomparable FROM tlas_comparables.mainIDs); 

注:the_id_columnsuperIDsthe_idcolumn_ofcomparableMainIds

+0

我認爲atlas_comparables是一個數據庫,superIDs和mainIDs是兩個表名 – fthiella

+0

我認爲你是對的,我會更新 –

0

如果只出現了IDS(最多),一次在每個表中,那麼你就可以相對做到這一點容易與左外連接:

select si.* 
from atlas_comparables.superIDs si left join 
    atlas_comparables.mainIDs mi 
    on si.SuperID = mi.SubID 
where mi.SubId is NULL; 

如果您想比較所有科拉姆ns(在其他數據庫中爲except),則需要更復雜的查詢,其中包含on子句中的所有列。