Nar*_*esh 1 mysql sql-order-by left-join
我有查询提供错误的结果,我在这个查询中做了任何错误
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
Run Code Online (Sandbox Code Playgroud)
查询没有任何错误,但产生错误的结果.我正在使用Mysql.
对于nBoutiqueID = 1特定实例nViewCount的最大总和应该是455,但它给了95124.这是巨大的差异.谁知道为什么?
看来你得到了一个查询的笛卡尔积...尝试从你的子查询中获取你的SUM()值...
SELECT
b.nBoutiqueID,
b.sBoutiqueName,
b.Status,
bs.StatsViewCount,
ps.ProductViewCount,
ps.ProductLinkClickCount,
ps.ProductWishListCount,
ps.ProductReferredCount
FROM
boutique b
LEFT JOIN ( select nBoutiqueID, sum( nViewCount ) as StatsViewCount
from boutique_stats
group by nBoutiqueID ) bs
ON b.nBoutiqueID = bs.nBoutiqueID
LEFT JOIN ( select SUM(nViewCount) ProductViewCount,
SUM(nLinkClickCount) ProductLinkClickCount,
SUM(nWishListCount) ProductWishListCount,
SUM(nReferredCount) ProductReferredCount
from product_stats
group by nBoutiqueID ) ps
ON ps.nBoutiqueID=b.nBoutiqueID
WHERE
b.bDeleted = 0
ORDER BY
ps.nProductID DESC
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3259 次 |
| 最近记录: |