有关同一列

时间:2017-03-09 12:15:32

标签: mysql sql

如何修复此查询?

SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = '0'
  AND notification_sent_to = ?
  AND notification_category = 'ticket reply'
  AND notification_category = 'groupdoc'

我需要对结果过滤仅计算故障回复和groupdoc的通知类别。

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:2)

列不能同时具有2个值。我想你想要的是OR

SELECT COUNT(*) 
FROM user_notifications 
WHERE notification_status = 0 AND notification_sent_to=? 
   AND (notification_category = 'ticket reply' OR notification_category= 'groupdoc')

答案 1 :(得分:2)

特定列在给定行中只能有一个值。所以以下条件永远不会是真的

notification_category = 'ticket reply'AND notification_category= 'groupdoc'

当人们认为我需要值为'a'and行且值为'b'的行时,要使用的实际逻辑运算符为OR

SELECT COUNT(*) FROM user_notifications WHERE notification_status = '0' AND notification_sent_to=? AND
 (notification_category = 'ticket reply' OR notification_category= 'groupdoc')

答案 2 :(得分:1)

使用in

SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = 0 AND
      notification_sent_to = ? AND
      notification_category IN ('ticket reply', 'groupdoc');