我有一张桌子:
我想编写一个查询来为特定用户显示唯一的Adid:
SELECT DISTINCT Adid FROM Message WHERE (MesFrom =1 OR MesTo = 1) AND (MesFrom =2 OR MesTo = 2) ORDER BY ID DESC
这没关系
但是现在我想添加新消息的数量,所以我需要像
这样的东西SELECT Count(IF(New=1,1,0)) AS countNew FROM Message
但需要将其与之前的查询结合起来
输出必须如下:
43新3 44 NEW 1
请帮忙!)
答案 0 :(得分:2)
尝试
SELECT Adid, SUM(New = 1) new
FROM Message
WHERE (MesFrom = 1 OR MesTo = 1)
AND (MesFrom = 2 OR MesTo = 2)
GROUP BY Adid
输出:
| ADID | NEW | |------|-----| | 42 | 2 | | 43 | 3 | | 44 | 1 |
这是 SQLFiddle 演示
答案 1 :(得分:1)
SELECT DISTINCT Adid , Count(IF(New=1,1,0)) AS countNew
FROM Message
WHERE (MesFrom =1 OR MesTo = 1) AND (MesFrom =2 OR MesTo = 2)
ORDER BY ID DESC
请检查它是否正常工作。