Mysql查询,三个表用group by左连接
我的查询提供了错误的结果,我在这个查询中做错了什么吗?
SELECT b.nBoutiqueID ,
b.sBoutiqueName ,
b.Status ,
SUM(bs.nViewCount) nViewCount ,
SUM(ps.nViewCount) nProductViewCount,
SUM(ps.nLinkClickCount) nLinkClickCount ,
SUM(ps.nWishListCount) nWishListCount ,
SUM(ps.nReferredCount) nReferredCount
FROM boutique b
LEFT JOIN boutique_stats bs
ON b.nBoutiqueID=bs.nBoutiqueID
LEFT JOIN product_stats ps
ON ps.nBoutiqueID=b.nBoutiqueID
WHERE b.bDeleted =0
GROUP BY b.nBoutiqueID
ORDER BY ps.nProductID DESC
查询没有给出任何错误,但产生了错误的结果。我正在使用Mysql。
对于 nBoutiqueID=1 的特定实例,nViewCount 的最大总和应为 455,但它给出 95124。这是巨大的差异。有人知道为什么吗?
I have query which provide wrong result, am I doing any thig wrong in this query
SELECT b.nBoutiqueID ,
b.sBoutiqueName ,
b.Status ,
SUM(bs.nViewCount) nViewCount ,
SUM(ps.nViewCount) nProductViewCount,
SUM(ps.nLinkClickCount) nLinkClickCount ,
SUM(ps.nWishListCount) nWishListCount ,
SUM(ps.nReferredCount) nReferredCount
FROM boutique b
LEFT JOIN boutique_stats bs
ON b.nBoutiqueID=bs.nBoutiqueID
LEFT JOIN product_stats ps
ON ps.nBoutiqueID=b.nBoutiqueID
WHERE b.bDeleted =0
GROUP BY b.nBoutiqueID
ORDER BY ps.nProductID DESC
Query giving not any error, but producing wrong result. I'm Using Mysql.
For particular Instance for nBoutiqueID=1 the max sum of nViewCount should be 455, but it gives 95124. that is huge difference. any one know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在获得查询的笛卡尔积...尝试从子查询中获取 SUM() 值...
It appears you are getting a sort-of Cartesian product of the query... Try getting your SUM() values from your sub-queries...
你说“最大nViewCount应该是455,但它给出了95124”。
但在您的查询中,您有
SUM(bs.nViewCount) nViewCount,
。难道不应该是 MAX(bs.nViewCount) nViewCount, 吗?
You say "max nViewCount should be 455, but it gives 95124".
But in your query you have
SUM(bs.nViewCount) nViewCount,
.Shouldn't that be
MAX(bs.nViewCount) nViewCount,
?