mysql错误总和和结果总和

时间:2013-08-27 22:58:08

标签: mysql

我们有三个不同的下表...

用户

ID, 用户名 密码 电子邮件

user_clubs

ID, 用户身份, club_name

销售

ID, club_id, 量, admin_fees, DNT

我们希望将admin_fees的总和视为user_id的未完成(例如55),因此我们尝试了以下操作...

SELECT u.id, count(c.id), SUM(s.admin_fees) as total_admin_fees 
FROM users u
LEFT JOIN user_clubs c ON c.user_id = u.id
LEFT JOIN sales s ON s.club_id = c.id
WHERE u.id = 55
GROUP BY u.id;

但它的第一行返回值,所以平衡不正确,请帮忙解决。

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT   g.id, count(g.club_id), SUM(g.admin_fees) AS total_admin_fees 
FROM     (
            SELECT    u.id, c.id AS club_id, s.admin_fees
            FROM      users u
            LEFT JOIN user_clubs c ON c.user_id = u.id
            LEFT JOIN sales s ON s.club_id = c.id
            WHERE     u.id = 55
         ) AS g
GROUP BY g.id;