/    Sign up×
Community /Pin to ProfileBookmark

Can’t retrieve all results with LEFT JOIN

Dear users,
I have two tables: “cars” and “users”.
The query is this:
SELECT * FROM cars AS c LEFT JOIN users AS u ON c.uid = u.uid WHERE c.proven = 1 && (u.state = 1 || u.state = NULL)
I want to specify not all the cars have an owner, so c.uid can be 0.
I want to retrieve all the cars not owned (c.uid = 0) and all the cars where the owner has an approved account (u.state = 1).

I tried to add u.state = NULL to get also the cars not owned, because LEFT JOIN returns NULL in all users fields, but this doesn’t work.

How can I fix this issue?

Thanks

to post a comment

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 21.2018 — I think maybe you just need to change u.state=NULL to u.state IS NULL, due to the way SQL treats nulls. (select 1=1; will return true, while select null=null; will return null.)
Copy linkTweet thisAlerts:
@redagrauthorMay 21.2018 — Thanks a lot.

Now it works!
×

Success!

Help @redagr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...