gpt4 book ai didi

mysql - 来自两个连接表的计数显示相同,即使它们不应该

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:18 24 4
gpt4 key购买 nike

我有一个非常直接的查询,它选择几个公司名称并计算其他两个连接表的结果。出于某种原因,我得到的优惠数量与规则相同,即使该数量仅适用于规则。

例如我有 2 个报价和 6 个规则。返回结果均显示为6。

我错过了什么?

SELECT
business_profile.busID,
business_profile.busName,
COUNT(business_offers.ofr_id) AS cntOffers,
COUNT(business_rules.rule_id) AS cntRules
FROM business_profile
LEFT JOIN business_offers ON (business_offers.ofr_busID = business_profile.busID)
LEFT JOIN business_rules ON (business_rules.rule_busID = business_profile.busID)
WHERE business_profile.busID > 1
GROUP BY business_profile.busID
ORDER BY cntRules DESC, cntOffers DESC
LIMIT 20

最佳答案

使用 DISTINCT 这样您就不会多次计算同一个 ID。

SELECT
business_profile.busID,
business_profile.busName,
COUNT(DISTINCT business_offers.ofr_id) AS cntOffers,
COUNT(DISTINCT business_rules.rule_id) AS cntRules

关于mysql - 来自两个连接表的计数显示相同,即使它们不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43268973/

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