SQL查询中的列数

时间:2018-06-08 19:51:57

标签: mysql sql

我想把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

预期输出(订单BY ID DESC) 69 是最后一条记录。

[item_id] [num]
    69      4
    70      2

2 个答案:

答案 0 :(得分:1)

有根据的猜测说你想要一个<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="../Content/bootstrap.min.js" rel="stylesheet" /> <script src="../Scripts/bootstrap.min.js"></script> </head> <body> <form id="form1" runat="server"> <div class="btn-group btn-group-justified"> <div class="btn-group"> <asp:Button runat="server" CssClass="btn btn-default" Text="Home" /> </div> <div class="btn-group"> <asp:Button runat="server" CssClass="btn btn-default" Text="Cart" /> </div> <div class="btn-group"> <asp:Button runat="server" CssClass="btn btn-default" Text="Products" /> </div> <div class="btn-group"> <asp:Button runat="server" CssClass="btn btn-default" Text="Contact Us" /> </div> </div> </form> </body> </html>

JOIN

目前还不清楚是否需要子查询中的过滤条件。

答案 1 :(得分:1)

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

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