开发者

MySQL JOIN with blank/null values returns no results

开发者 https://www.devze.com 2023-03-27 07:28 出处:网络
I\'m trying to return some values optionally (if they exist) and if not return t开发者_运维知识库he rest of the set.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号