How to get query result even if JOIN hasn't found any results?
Posted
by
user1734651
on Stack Overflow
See other posts from Stack Overflow
or by user1734651
Published on 2012-10-28T16:56:42Z
Indexed on
2012/10/28
17:00 UTC
Read the original article
Hit count: 374
I want select data for user, and join another info from other table that related to the user. The problem is that this extra data not always exist for any user, just for few.
How can I write a query that will return NULL for not found data, instead just return null for the whole query?
SELECT a.*, b.*
FROM user AS a
LEFT JOIN extra AS b ON (a.userid = b.userid)
WHERE a.userid = {$userid}
LIMIT 1
When extra data found for the user, I get the resource as expected. If not, I get NULL for the whole query.
Bottom line, I don't care if "extra" exist for the user or not, if yes - select it as well, if not - ignore that.
© Stack Overflow or respective owner