0

我对使用 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

4 回答 4

3

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

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

或者正如 Martin Smith 所指出的,在你的情况下,使用IN

AND cdr.userId IN  ('U4BC3128209B01','U4A9FCCD73C6BB')
于 2012-11-02T16:10:02.627 回答
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,所以没有混乱

于 2012-11-02T16:11:24.070 回答
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  ;
于 2012-11-02T16:11:31.480 回答
0

在 SQL 中 AND 具有更高的顺序然后 OR 所以默认情况下你的 where 解析为

             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')  

使用括号,它会清除的东西。

于 2012-11-02T16:10:24.280 回答