我想在此语句中放入COUNT(item_id)

SELECT * FROM `notifications` WHERE `uid` = '3' AND `seen` = '0' AND id IN (
SELECT MAX(id), COUNT(item_id)
FROM `notifications`
GROUP BY item_id
)  ORDER BY id DESC


但是发生此错误:操作数应包含1列。

表:

[id] [uid] [item_id] [seen]
 1     3       69       0
 2     3       69       0
 3     3       70       0
 4     3       69       0
 5     3       70       0
 6     3       69       0


预期输出:(Order BY id DESC),其中69是最后一条记录。

[item_id] [num]
    69      4
    70      2

最佳答案

给定您的样本数据和预期结果,就不需要子查询:

select item_id, count(*)
from notifications
group by item_id
where uid = 3 and seen = 0
order by max(id) desc;



Sample Demo

关于mysql - SQL查询中的列数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50767573/

10-13 02:27