错误的结果(或哪里查询)

时间:2021-10-30 06:22:31

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:

重要的认为是游戏。winner_user_id ! = 49。但我的查询结果是:

错误的结果(或哪里查询)

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

有人可能会告诉我,为什么我收到winner_user_id 49的结果,但是我想不等于。谢谢

3 个解决方案

#1


2  

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

您可能需要将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)

#2


2  

ANDhas precedence over OR. Hence your query equals:

并且优先于或。因此您的查询等于:

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;

where you want it to be

你想把它放在哪里

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

Conclusion: Use parentheses whenever mixing AND and OR.

结论:混合或混合时使用圆括号。

#3


0  

Because you are using or clause in your query. Update your query as per your requirement. If your just want to don't show record of winner id 49. You can use below query.

因为您正在查询中使用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)

#1


2  

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

您可能需要将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)

#2


2  

ANDhas precedence over OR. Hence your query equals:

并且优先于或。因此您的查询等于:

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;

where you want it to be

你想把它放在哪里

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

Conclusion: Use parentheses whenever mixing AND and OR.

结论:混合或混合时使用圆括号。

#3


0  

Because you are using or clause in your query. Update your query as per your requirement. If your just want to don't show record of winner id 49. You can use below query.

因为您正在查询中使用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)