下面2句sql差别就是一对括号,我本以为都可达到相同的目的,但是查询出记录不同,谁能给我解释一下呢?1.
select count(distinct taxpayernum) count
  from ds_user u
  left join ds_stamp e on u.taxpayernum = e.tpayer_id
  left join ds_zero_decl d on u.taxpayernum = d.taxper_id
 where e.stamp_id is not null
    or d.zero_decl_seq is not null
   and u.isparent = '1'
   and u.usertype = '0'
   and u.isinuse in ('5', '1')2.
select count(distinct taxpayernum) count
  from ds_user u
  left join ds_stamp e on u.taxpayernum = e.tpayer_id
  left join ds_zero_decl d on u.taxpayernum = d.taxper_id
 where (e.stamp_id is not null
    or d.zero_decl_seq is not null)
   and u.isparent = '1'
   and u.usertype = '0'
   and u.isinuse in ('5', '1')

解决方案 »

  1.   

    这是逻辑操作选先级的问题。 where   e.stamp_id   is   not   null 
            or   d.zero_decl_seq   is   not   null 
          and   u.isparent   =   '1 ' 
          and   u.usertype   =   '0 ' 
          and   u.isinuse   in   ( '5 ',   '1 ') 等于:
    where   e.stamp_id   is   not   null 
            or   (d.zero_decl_seq   is   not   null 
          and   u.isparent   =   '1 ' 
          and   u.usertype   =   '0 ' 
          and   u.isinuse   in   ( '5 ',   '1 ') )
    明白了吧。
      

  2.   

    or的优先级比and要大,也就是在不加括号的情况下,解析where子句时先匹配or,如果有or,就将or两侧的语句分开解析
      

  3.   

    我想应该是and比or的优先级大吧?
    先计算and后匹配or,所以LZ的疑惑就解开了