我正在尝试选择已准确订购1个订单的1000个客户。一切都在Orders表中。
从
Order
中选择*计数(CustomerID)= 1限制1000
基本上所有记录在整个表中只有一个CustomerID出现。这将返回一个空结果集,表中有100,000个。
答案 0 :(得分:4)
您需要GROUP
才能(有意义地)使用HAVING
子句。以下按预期工作:
SELECT
*
FROM
`Order`
GROUP BY
CustomerID
HAVING
COUNT(CustomerID) = 1
LIMIT 1000
<强>更新强>
添加WHERE
(参见评论):
SELECT
*
FROM
`Order`
WHERE
Language = 'EN'
GROUP BY
CustomerID
HAVING
COUNT(CustomerID) = 1
LIMIT 1000
答案 1 :(得分:1)
SELECT * FROM `Order`
GROUP BY CustomerID
HAVING COUNT(CustomerID) = 1
LIMIT 1000
答案 2 :(得分:0)
试
select count(CustomerID) as counter ,o.* from Order o
group by CustomerID having counter = 1 limit 1000
答案 3 :(得分:0)
添加group by以查询正确的输出,例如:
选择* 来自订单 按客户ID分组 有计数(CustomerID)= 1 限制1000
答案 4 :(得分:0)
SELECT CustomerID FROM Order
GROUP BY CustomerID HAVING COUNT(*)= 1 LIMIT 1000