我在计数列上遇到了mysql ROLLUP。你能指导我吗?
我写了以下mysql查询
查询:
SELECT (SELECT store.StoreName from store
WHERE store.storeOwnerId=redeem.StoreNo)
AS CompanyName,
(SELECT mscheme.SchemeName from mscheme
WHERE mscheme.SchemeNo = redeem.SchemeNo)
AS SchemeName,
count(*) AS Count
FROM kirana_app.redeem GROUP BY redeem.SchemeNo
ORDER BY CompanyName;
**输出**
CompanyName,SchemeName, Count
'Dwarkesh', 'September-saffola', '25'
'Dwarkesh', 'September-Tea', '20'
'Nitin stores', 'Testing Scheme', '1'
'Nitin stores', 'September-Parle', '63'
我需要使用Count列和
进行汇总输出如:
CompanyName,SchemeName, Count
'Dwarkesh', 'September-saffola', '25'
'Dwarkesh', 'September-Tea', '20'
**Dwarkesh', NULL, '45'**
'Nitin stores', 'Testing Scheme', '1'
'Nitin stores', 'September-Parle', '63'
**'Nitin stores', NULL, '64**'
答案 0 :(得分:0)
学习正确连接而不是子查询。
SELECT s.StoreName AS CompanyName,
m.SchemeName AS SchemeName,
COUNT(*) AS Count
FROM redeem r
INNER JOIN store s ON r.StoreNo = s.storeOwnerId
INNER JOIN mscheme m ON m.SchemeNo = r.SchemeNo
GROUP BY s.CompanyName, m.SchemeNo
WITH ROLLUP;
ORDER BY
由GROUP BY
隐含地完成。