是否有一些有效的方法来编写在公共表上连接GROUP BY的各种结果的查询? MySQL如何处理将subGROUP上的aggreagate函数的结果与原始表中的完整字段合并?
我正在使用它并且它很慢(我还需要除CONDITION = 1之外的其他条件)
SELECT a.CID,a.AS_ALL,b.AS_ACTIVE FROM
(SELECT CID,COUNT(DISTINCT RAID) AS AS_ALL FROM MYTABLE GROUP BY CID) a
LEFT JOIN
(SELECT CID,COUNT(DISTINCT RAID) AS AS_ACTIVE FROM MYTABLE WHERE CONDITION=1 GROUP BY CID ) b ON a.CID=b.CID;
也可以使用像? MySQL总是会正确地将COLUMN_A与聚合结果合并吗?
SELECT COLUMN_A COUNT(DISTINCT COLUMN_A), SUM(COLUMN_A),SUM(COLUMN_B) FROM ATABLE WHERE CONDITION=1 GROUP BY COLUMN_C
感谢您的建议
答案 0 :(得分:0)
试试这个:
SELECT CID,COUNT(DISTINCT RAID) AS AS_ALL,SUM(IF(CONDITION=1, 1, 0)) AS AS_ACTIVE
FROM MYTABLE GROUP BY CID