错误的结果(或查询)

时间:2016-05-04 11:30:52

标签: mysql sql

我的查询如下:

work()

重要的想法是游戏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 winner_user_id`!= 49 。但我查询的结果是:

enter image description here

有人可以告诉我,为什么我收到了win_user_id 49的结果,但我不想等于。 感谢

3 个答案:

答案 0 :(得分:2)

您可能必须在括号中包含WHERE子句的最后两个谓词:

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)

答案 1 :(得分:2)

AND优先于OR。因此,您的查询等于:

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;

你希望它在哪里

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);

结论:只要混合ANDOR,就可以使用括号。

答案 2 :(得分:0)

因为您在查询中使用or子句。根据您的要求更新您的查询。如果您只是想不显示获胜者ID 49的记录。您可以使用以下查询。

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)