【问题标题】:more than one where condition in sql querysql查询中的多个where条件
【发布时间】:2015-04-13 04:09:51
【问题描述】:

我正在尝试在 pgSQL 中执行以下查询。

   select count(id) from master 
        where act1=true or act2=true
         and regn_no>2000;

但查询返回满足“act1=true 或 act2=true”条件的所有行。它不检查 act1=true 或 act2=true。为什么?

SQLFIDDLE: http://sqlfiddle.com/#!9/23b1f/1

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    因为AND 优先级高于OR 然后sql 将你的where 子句解析为

    (act1=true) or (act2=true
             and regn_no>2000);
    

    你必须重写你的查询如下

    select count(id) from master 
            where (act1=true or act2=true)
             and regn_no>2000;
    

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      相关资源
      最近更新 更多