2013-02-20 57 views
0

我想返回table1中所有活動的郵政編碼,並且沒有共享相同座標(lat, LNG)。 也就是說在下面的回報:返回一個MySQL表中沒有出現在其他列中的項目,其中有兩列要考慮

AB11AC 

我知道有幾個方法,你只是檢查一個欄,但不知道如何爲2列適應。我應該只在查詢中連接2列嗎?還是有更高效的方法?我的表格每個都有大約200萬個條目。

表1:

postcode lat lng active 
------------------------- 
AB11AA 55 1 Y 
AB11AB 56 1 Y 
AB11AC 57 1 Y 

表2:

postcode lat lng active 
-------------------------- 
AB11AA 55 1 Y 
AB11AD 56 1 Y 
AB11AE 59 1 Y 

回答

1

您可以使用LEFT JOIN

select * 
from table1 t1 
left join table2 t2 
    on t1.lat = t2.lat 
    and t1.lng = t2.lng 
where t1.active = 'Y' 
    and t2.postcode is null 

SQL Fiddle with Demo

或者你可以使用一個NOT EXISTSWHERE子句中:

select * 
from table1 t1 
where t1.active = 'Y' 
    and not exists (select * 
        from table2 t2 
        where t1.lat = t2.lat 
        and t1.lng = t2.lng) 

SQL Fiddle with Demo

相關問題