Jędrzej Babiarz Jędrzej Babiarz - 7 months ago 12
SQL Question

Wrong result of (or where query)

My query look like:

select *
from `games`
inner join `prowizja` on `prowizja`.`id_gry` = `games`.`id`
where `games`.`status` = 2 and `games`.`winner_user_id` != 49
and `games`.`owner_user_id` = 49 or `games`.`join_user_id` = 49


Important think is games
.
winner_user_id` != 49
. But I result of this query up is:

enter image description here

Someone could tell me, why I receive result with winner_user_id 49, but I want not equals.
Thanks

Answer

You probably have to wrap the last two predicates of the WHERE clause in parentheses:

select * 
from `games` 
inner join `prowizja` 
on `prowizja`.`id_gry` = `games`.`id`
where `games`.`status` = 2 and 
      `games`.`winner_user_id` != 49 and
      (`games`.`owner_user_id` = 49 or `games`.`join_user_id` = 49)