缩短mysql案例查询

时间:2017-06-22 11:32:32

标签: mysql sql

我已经编写了这个查询,但它似乎是一个非常糟糕的查询,

select * from games_applied where  
 games_post_id='1126' and status != '4' and
 (status != '5' and rejected_status = '0' or status = '5' and rejected_status = '1');

如何更好地利用这种情况。

(status != '5' and rejected_status = '0' or status = '5' and rejected_status = '1')

应仅显示 rejected_status = 1且状态= 5 的行 并且不应显示 rejected_Status = 0且状态= 5

的行

3 个答案:

答案 0 :(得分:1)

这不是一个主要的简化,但确实删除了与Dense的比较。但是,这就是我编写查询的方式:

4

注意:

  • 该表有一个有意义的表别名。
  • 所有列名都是合格的。
  • 我认为这些数字实际上是数字列的比较,因此我删除了单引号。
  • 我将比较删除到" 4"。

答案 1 :(得分:1)

您的查询所需的唯一改进是将两个子句封装在或语句的任一侧。换句话说,将statusrejected_status检查放在括号内。

select 
    * 
from 
    games_applied 
where  
    games_post_id='1126' 
    and status != '4' 
    and
    (
        (status != '5' and rejected_status = '0') 
        or (status = '5' and rejected_status = '1')
    );

答案 2 :(得分:0)

以下是查询,希望它可以按要求运行:

select * from games_applied where  
games_post_id='1126' and status != '4' and case when status='5' then 
rejected_status='1' else rejected_status='0' end;
相关问题