我有这样的疑问:
SELECT price FROM orders WHERE user_id = 10 HAVING count(*) > 1
它可以工作,但总是只返回一行。我做错了什么?
答案 0 :(得分:0)
由于HAVING
条款适用于GROUP BY
,因此请参阅此处description
如评论中所述: -
我需要获得特定用户所做的所有订单(价格)
您无需使用HAVING
,您可以这样做:
SELECT price FROM orders WHERE user_id = 10
答案 1 :(得分:0)
HAVING
是GROUP BY
的扩展程序。它不能单独使用。文档here
答案 2 :(得分:0)
您可以使用没有分组数据,例如:
SELECT price FROM orders WHERE user_id = 10 HAVING price > 10
但是如果你使用像count,sum等分组数据,你应该添加group by
:
SELECT sum(price) FROM orders WHERE user_id = 1 group by user_id HAVING count(*) > 1;
答案 3 :(得分:0)
SELECT
o.price,
o.user_id
FROM orders o
INNER JOIN(
SELECT
user_id
FROM orders
WHERE
user_id = 10
GROUP BY user_id
HAVING count(*) > 1
) subQ
ON subQ.user_id = o.user_id
答案 4 :(得分:0)
仅显示特定ID多次出现的记录(来自This Thread
SELECT * FROM table WHERE ID IN (SELECT ID FROM table GROUP BY ID HAVING COUNT(*) > 1)
在您的情况下,您希望显示特定ID的订单价格。首先,我们选择user_id并限制“多次购买”条件。 然后我们将其注入正常请求
SELECT prices FROM orders
WHERE user_id = 10
AND user_id IN
(SELECT user_id FROM orders GROUP BY user_id HAVING COUNT(*) > 1);
这应该有效