Join

使用 WHERE 左連接 (Microsoft Access SQL)

  • April 28, 2019

給定以下2個表a,b:

表一

ID job
1  farmer
2  physician
3  bank clerk

表b

bID ID lang job
1   1  en   farmer
2   2  en   physician
3   3  en   bank clerk
4   2  de   Arzt
5   1  fr   paysan

詢問

SELECT a.ID, a.job, b.job FROM a LEFT JOIN b ON a.ID=b.ID WHERE b.lang='de'

通緝:

a.ID a.job        b.job
1    farmer       -
2    physician    Arzt
3    bank clerk   -

實際結果:

2    physician    Arzt

有什麼問題LEFT JOIN?我需要一些(())嗎?

在您的查詢中,僅使用條件執行左連接a.ID=b.ID,然後根據條件過濾結果b.lang='de'。您需要在左連接上添加兩個條件:

SELECT a.ID, a.job, b.job 
FROM a LEFT JOIN b 
ON (a.ID=b.ID and b.lang='de')

在這裡創建特定語言表的別名為c

SELECT a.ID, a.job, c.job FROM a
LEFT JOIN 
(SELECT * FROM b WHERE lang='de') as c
ON a.ID=c.ID

引用自:https://dba.stackexchange.com/questions/215326