2014-03-25 24 views
0

行不常見的我有兩個表:如何檢索兩個表

tata_data1:

Password | Region | Company | System 
------------------------------------- 
a02040 | Del | xx  | abc 
a01917 | Mum | xxx  | pqr 
a01916 | Mum | x  | zzz 
a01906 | Nag | x  | pny 

tata_passwords:

Password | Region | Company 
---------------------------- 
a02049 | Nag | xxxx 
a01917 | Mum | xxx 
a01000 | Del | xx 
a01906 | Nag | x 

我想只有那些獲取來自tata_passwords的行,它們不在tata_data1中。考慮將密碼作爲主鍵。

回答

0

試試這個:

SELECT * FROM tata_passwords WHERE (Password, Region, Company) NOT IN (SELECT Password, Region, Company FROM tata_data1) 

編輯:

現在,當密碼是主鍵,一個可以在查詢簡化爲:

SELECT * FROM tata_passwords WHERE Password NOT IN (SELECT Password FROM tata_data1) 
+0

工作!謝謝 :) – Niks

0
select * 
from tata_passwords 
where tata_passwords.password NOT IN (select password from tata_data1) 

或者你可以使用Mr.KickstartLEFT OUTER JOIN

SELECT tata_passwords.* 
FROM tata_passwords LEFT OUTER JOIN tata_data1 ON tata_passwords.password=tata_data1.password 
where tata_data1.password IS NULL 
1

使用LEFT OUTER JOIN: -

SELECT tata_passwords.* 
FROM tata_passwords 
LEFT OUTER JOIN tata_data1 
ON tata_passwords.Password = tata_data1.Password 
WHERE tata_data1.Password IS NULL