Nagy Wesley
Nagy Wesley

Reputation: 121

How to join two tables on a condition that may not occur?

I have two tables

How to select users that have optionName = 'email' and value = 1 or have no entries for email in the Options table?

Upvotes: 1

Views: 57

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172578

Try this:

select * from User u inner join Option o on u.id = o.userid
where (o.optionName = 'email' and o.value = 1) or (o.optionName <> 'email')

Upvotes: 2

Sanjay Kumar N S
Sanjay Kumar N S

Reputation: 4749

THis might help you:

"SELECT * from users u left join options o on u.id = o.userid where (o.optionName = 'email' and o.value = 1) or o.userid is null; ";

Upvotes: 0

Related Questions