MySQL JOIN with blank/null values returns no results
I'm trying to return some values optionally (if they exist) and if not return t开发者_运维知识库he rest of the set.
SELECT people.first_name, countries1.name AS "Country1"
FROM addressbook_people AS people
JOIN root_countries AS countries1 ON people.country1 = countries1.id
On some cases there will be no value supplied for people.country1,
however if one it not supplied no results.How would I restructure this query to still return people.first_name when there is no value in people.country1?
Just do
SELECT people.first_name, countries1.name AS "Country1"
FROM addressbook_people AS people
LEFT JOIN root_countries AS countries1 ON people.country1 = countries1.id
which will result to returning NULL
on the respective fields of the root_countries
table.
精彩评论