【问题标题】:SQL multiple where conditions not workingSQL 多个 where 条件不起作用
【发布时间】:2013-12-31 06:53:24
【问题描述】:

我有这 3 个查询,除了 where 语句之外几乎相同:

Insert into t1(f1,f2,f3)
SELECT     t2.f1 AS f1, '' AS f2, t2.f2 AS f2, t3.f3 AS f3)                                                                                    
from  t2 
INNER JOIN t3 ON t2.f1 = t3.f1                       
WHERE     (t2.f4 = 'n') and t2.f5 < '2013-04-01' and t2.f6 in('sss','ttt','ddd')
GROUP BY t2.f4, t2.f5, t2.f6;



Insert into t1(f1,f2,f3)
SELECT     t2.f1 AS f1, '' AS f2, t2.f2 AS f2, t3.f3 AS f3)                                                                                    
from  t2 
INNER JOIN t3 ON t2.f1 = t3.f1                       
WHERE     (t2.f4 = 'n') and t2.f5 > '2013-03-31' and t2.f6 in('sss','ttt','ddd')
GROUP BY t2.f4, t2.f5, t2.f6;


Insert into t1(f1,f2,f3)
SELECT     t2.f1 AS f1, '' AS f2, t2.f2 AS f2, t3.f3 AS f3)                                                                                    
from  t2 
INNER JOIN t3 ON t2.f1 = t3.f1                       
WHERE     (t2.f4 = 'n')  and t2.f6 in('rrr','qqq','yyy')
GROUP BY t2.f4, t2.f5, t2.f6;

我不能像下面的查询一样将它们组合起来,理论上它们是相同的查询吗?因为我完全尝试过这个,当我测试结果时我得到了不同的输出。

   Insert into t1(f1,f2,f3)
   SELECT     t2.f1 AS f1, '' AS f2, t2.f2 AS f2, t3.f3 AS f3)                                                                                    
   from  t2 
   INNER JOIN t3 ON t2.f1 = t3.f1    
   WHERE (t2.f4 = 'n') and (t2.f5 < '2013-04-01' or t2.f5 > '2013-03-31' and t2.f6 in         
   ('sss','ttt','ddd'))  or t2.f6 in ('rrr','ggg','yyy')
    GROUP BY t2.f4, t2.f5, t2.f6;

我也试过这个,但它也不匹配(我几乎只是将前面的每一个 where 语句放在括号中,并在组合语句中添加了一个或):

 Insert into t1(f1,f2,f3)
   SELECT     t2.f1 AS f1, '' AS f2, t2.f2 AS f2, t3.f3 AS f3)                                                                                    
   from  t2 
   INNER JOIN t3 ON t2.f1 = t3.f1
   WHERE     ((t2.f4 = 'n') and t2.f5 < '2013-04-01' and t2.f6 in('sss','ttt','ddd')) or
((t2.f4 = 'n') and t2.f5 > '2013-03-31' and t2.f6 in('sss','ttt','ddd')) or ((t2.f4 = 'n')  and t2.f6 in('rrr','qqq','yyy'))

【问题讨论】:

    标签: mysql sql where-in


    【解决方案1】:

    它们不是同一个查询,因为 AND 优先于 OR。

    所以在你第一次尝试重写时,你有:

    (t2.f4 = 'n')
    and  (t2.f5 < '2013-04-01' or t2.f5 > '2013-03-31' and t2.f6 in ('sss','ttt','ddd'))
    or t2.f6 in ('rrr','ggg','yyy')
    

    满足第三行的任何东西都满足整个 where 子句。

    换句话说,你需要在前两行加上括号:

    ((t2.f4 = 'n')
    and  (t2.f5 < '2013-04-01' or t2.f5 > '2013-03-31' and t2.f6 in ('sss','ttt','ddd')))
    or t2.f6 in ('rrr','ggg','yyy')
    

    这可能会有所帮助:SQL Logic Operator Precedence: And and Or

    【讨论】:

      【解决方案2】:

      这不行吗?:

      Insert into t1(f1,f2,f3)
      SELECT     t2.f1 AS f1, '' AS f2, t2.f2 AS f2, t3.f3 AS f3)                                                                                    
      from  t2 
       INNER JOIN t3 ON t2.f1 = t3.f1                       
      WHERE     ((t2.f4 = 'n') and t2.f5 > '2013-03-31' and t2.f6 in('sss','ttt','ddd'))
      OR     ((t2.f4 = 'n') and t2.f5 < '2013-04-01' and t2.f6 in('sss','ttt','ddd'))
      OR     ((t2.f4 = 'n')  and t2.f6 in('rrr','qqq','yyy'))
      GROUP BY t2.f4, t2.f5, t2.f6;
      

      【讨论】:

        猜你喜欢
        • 2022-01-04
        • 2020-04-22
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-11
        • 2021-07-07
        • 1970-01-01
        相关资源
        最近更新 更多