带有空值/空值的MySQL JOIN不返回任何结果

时间:2011-08-09 13:56:27

标签: mysql join null

我正在尝试可选地返回某些值(如果它们存在),如果不返回该组的其余部分。

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.first_name 当people.country1中没有值时?

1 个答案:

答案 0 :(得分: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

将导致在NULL表的相应字段上返回root_countries