日期:2014-05-16  浏览次数:20891 次

sql 分组统计问题
SQL code
SELECT COUNT( a.member_id ) AS sum, a.member_id
FROM sdb_orders AS a
LEFT JOIN sdb_members AS b ON ( a.member_id = b.member_id ) 
WHERE a.status <>  'dead'
GROUP BY a.member_id
HAVING COUNT( * ) >1



比如查询出来的结果
member_id sum
1 3
2 2

我要的结果是对COUNT( a.member_id )相加 结果就是5

------解决方案--------------------
SQL code

SELECT sum( a.member_id ) AS total, a.member_id
FROM sdb_orders AS a
LEFT JOIN sdb_members AS b ON ( a.member_id = b.member_id ) 
WHERE a.status <>  'dead'
GROUP BY a.member_id
HAVING COUNT( * ) >1