Dylano236
Dylano236

Reputation: 317

MySQL query showing rows that should be omited

So I don't know what I'm missing here? When I run this query I'm getting rows with a license_id that are not 3767 along with the correct rows that have 3767. If I say "where users_licenses.license_id = 3767" I should only receive records with a license_id of 3767 correct?

select * from users 
join users_licenses on users_licenses.user_id = users.id 
join groups on groups.id = users.group_id
WHERE  users_licenses.license_id = 3767 and groups.id = 6325 or groups.id = 6343

Now it's something with the or statement because when it's just one group ID it works. But still why should the or in the second where clause matter? I should still only receive rows with a license_id of 3767

Upvotes: 0

Views: 24

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

Use parentheses, until you get used to how and and or work. Your where clause is parsed as:

WHERE (users_licenses.license_id = 3767 and groups.id = 6325) or
      groups.id = 6343

which does not seem to be your intention.

However, you can simplify your filtering in this case using in:

where users_licenses.license_id = 3767 and groups.id in (6325, 6343)

Upvotes: 2

Related Questions