2011-08-09 138 views
3

我試圖返回一些值(如果它們存在),如果不返回該集合的其餘部分。與空白/空值加入MySQL返回沒有結果

SELECT people.first_name, countries1.name AS "Country1" 
FROM addressbook_people AS people 
JOIN root_countries AS countries1 ON people.country1 = countries1.id 

在某些情況下,不會有任何的people.country1提供的值,
但是如果一個它不提供任何結果。

如何重構此查詢,以便在people.country1中沒有值時仍返回people.first_name ?

+2

使用左連接。 –

+1

可能會將'JOIN'改爲'LEFT JOIN' – ajreal

回答

5

只是做

SELECT people.first_name, countries1.name AS "Country1" 
FROM addressbook_people AS people 
LEFT JOIN root_countries AS countries1 ON people.country1 = countries1.id 

這將導致久違的root_countries表的相應字段NULL