gpt4 book ai didi

结果 "greater than or equal to"的 SQL COUNT 函数

转载 作者:行者123 更新时间:2023-12-04 20:16:45 25 4
gpt4 key购买 nike

我目前有一个查询,它返回每个客户持有的帐户总数,但我如何使它只返回拥有超过 1 个帐户的客户?

SELECT C.customerID, COUNT(O.accNumber) AS "total"
FROM Customer C, Owns O
WHERE C.customerID = O.customerID
GROUP BY C.customerID

最佳答案

您的问题的答案是HAVING .但是,您需要学会使用正确的JOIN句法。简单规则:切勿在 FROM 中使用逗号条款。始终使用显式 JOIN句法。

SELECT C.customerID, COUNT(O.accNumber) AS total
FROM Customer C JOIN
Owns O
ON C.customerID = O.customerID
GROUP BY C.customerID
HAVING COUNT(*) > 1;

实际上,您甚至不需要 JOIN :
SELECT o.customerID, COUNT(o.accNumber) AS total
FROM Owns o
GROUP BY o.customerID
HAVING COUNT(*) > 1;

这要简单得多。

关于结果 "greater than or equal to"的 SQL COUNT 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com