MySQL Where子句不过滤数据

时间:2018-04-10 11:16:35

标签: mysql

我正在尝试执行以下查询。查询正在工作但不过滤数据作为where子句。我无法弄清楚问题

提前致谢!

 SELECT
    case when count(*) = 1 then
    (select chacc from xxx_1 s inner join xxx_2 d on s.MCHRRN = rrn(d)
     where d.CHCHNO = dp.CHCHNO and d.CHBKBR = dp.CHBKBR AND d.CHBRCH = dp.CHBRCH AND
     s.MCHPNDT = st.MCHPNDT)
    else 1 end  as is_multiple,
    count(*) as ch_tot,
    dp.CHBRCH,
    dp.CHCHNO,
    dp.CHBKBR,
    SUM(dp.CHCHAM) as chq_amt_tot,
    st.MCHPNDT,
    dp.CHCHST
FROM xxx_3 st
INNER JOIN xxx_4 dp
    ON st.MCHRRN = RRN(dp)
WHERE st.MCHSTAT='P' AND st.MCHAPP IN (40,50) OR (st.MCHAPP = 26 AND st.MCHSVST = 1) AND
    dp.CHDWAC=29 AND dp.CHCHNO=5261 AND st.MCHPNDT BETWEEN 2018001 AND 2018100
GROUP BY dp.CHBRCH,dp.CHCHNO,dp.CHBKBR,dp.CHCHST,st.MCHPNDT
ORDER BY st.MCHPNDT ASC

1 个答案:

答案 0 :(得分:1)

我怀疑你只需要括号:

WHERE ( (st.MCHSTAT = 'P' AND st.MCHAPP IN (40, 50) ) OR
        (st.MCHAPP = 26 AND st.MCHSVST = 1)
      ) AND
      dp.CHDWAC = 29 AND
      dp.CHCHNO = 5261 AND
      st.MCHPNDT BETWEEN 2018001 AND 2018100

如果您在布尔表达式中同时使用ANDOR,我建议您始终使用括号。