与ANDs的SQL混淆

时间:2012-11-02 16:07:27

标签: sql

我对使用AND的SQL查询感到困惑。

查询如下:

SELECT  count(cdr.id) as callTotal,
    sum(cdr.callDurationSeconds) as timeTotal,
    users.firstname,
    users.surname, 
    MAX(cdr.callDuration) as maxCallDuration,
    AVG(cdr.answeredTime) as avgAnswerTime,
    AVG(cdr.callDurationSeconds) as avgTimeTotal,
    cdr.userId, 
    COUNT(IF(cdr.callDurationSeconds > '0', 1, NULL)) AS answeredCalls
                 FROM cdr 
                 LEFT JOIN users ON cdr.userId = users.id
                 WHERE cdr.company = 'C47B0BCCDCE92F'
                 AND cdr.dateCreated LIKE '2012-11-02%'  
                 AND  cdr.userId = 'U4BC3128209B01'  
   OR  cdr.userId = 'U4A9FCCD73C6BB'  
   GROUP BY userId  ORDER BY users.surname ASC  ;

我想查询第二行到最后一行的日期,但是我希望检索任何已发布的cdr.userIds的项目。 但是在这种情况下似乎无视日期。我需要将日期设置为非可选项,并在所选日期使用选择ID检索任何数据。

4 个答案:

答案 0 :(得分:3)

如果您不想在SQL中使用OR / AND运算符优先级(这是一个相当糟糕的主意),只需添加括号。

AND  (cdr.userId = 'U4BC3128209B01'  OR  cdr.userId = 'U4A9FCCD73C6BB')

或如Martin Smith所指出,在您的情况下,请使用IN

AND cdr.userId IN  ('U4BC3128209B01','U4A9FCCD73C6BB')

答案 1 :(得分:1)

您可以像其他提到的那样用括号括起OR或像这样使用IN

SELECT  count(cdr.id) as callTotal,
                         sum(cdr.callDurationSeconds) as timeTotal,
                         users.firstname,
                         users.surname, 
                         MAX(cdr.callDuration) as maxCallDuration,
                         AVG(cdr.answeredTime) as avgAnswerTime,
                         AVG(cdr.callDurationSeconds) as avgTimeTotal,
                         cdr.userId, 
                         COUNT(IF(cdr.callDurationSeconds > '0', 1, NULL)) AS answeredCalls
                 FROM cdr 
                 LEFT JOIN users ON cdr.userId = users.id
                 WHERE cdr.company = 'C47B0BCCDCE92F'
                 AND cdr.dateCreated LIKE '2012-11-02%'  
                 AND  cdr.userId IN ('U4BC3128209B01','U4A9FCCD73C6BB')  GROUP BY userId  ORDER BY users.surname ASC  ;

这样就没有OR,所以没有混淆

答案 2 :(得分:1)

如果我理解正确,您想要选择具有该日期且具有特定ID的项目?

SELECT  count(cdr.id) as callTotal,
                         sum(cdr.callDurationSeconds) as timeTotal,
                         users.firstname,
                         users.surname, 
                         MAX(cdr.callDuration) as maxCallDuration,
                         AVG(cdr.answeredTime) as avgAnswerTime,
                         AVG(cdr.callDurationSeconds) as avgTimeTotal,
                         cdr.userId, 
                         COUNT(IF(cdr.callDurationSeconds > '0', 1, NULL)) AS answeredCalls
                 FROM cdr 
                 LEFT JOIN users ON cdr.userId = users.id
                 WHERE cdr.company = 'C47B0BCCDCE92F'
                 AND cdr.dateCreated LIKE '2012-11-02%'  
                     AND  cdr.userId IN('U4BC3128209B01','U4A9FCCD73C6BB')
                 GROUP BY userId  ORDER BY users.surname ASC  ;

答案 3 :(得分:0)

在SQL中有一个更高的顺序然后OR,所以默认情况下你的地方解析为

             WHERE (cdr.company = 'C47B0BCCDCE92F'
             AND cdr.dateCreated LIKE '2012-11-02%'  
             AND  cdr.userId = 'U4BC3128209B01')  OR  cdr.userId = 'U4A9FCCD73C6BB'  

你想要的地方

             WHERE cdr.company = 'C47B0BCCDCE92F'
             AND cdr.dateCreated LIKE '2012-11-02%'  
             AND  (cdr.userId = 'U4BC3128209B01'  OR  cdr.userId = 'U4A9FCCD73C6BB')  

使用括号,它会清除。