這是tbl_user:SUM MySQL表的多個列的
-----------------
ID | Username
-----------------
1 user one
2 user two
這是tbl_x1:
-------------------------
ID | User_id | Sum
-------------------------
1 1 10
2 1 20
3 2 30
這是tbl_x2:
-------------------------
ID | User_id | Sum
-------------------------
1 1 10
2 1 20
3 1 30
這是tbl_y:
-------------------------
ID | User_id | Days
-------------------------
1 1 10
我想選擇sum(x1.sum + x2.sum)和sum(y.Days)。
換句話說,我想以下結果:
-------------------------------------
ID | Username | Sum | Days
-------------------------------------
1 user one 90 10
2 user two 30 0
我這個代碼的嘗試:
select u.id as ID, u.username as Username, sum(y.days) as Days,(ifnull(sum(x1.sum), 0) + ifnull(sum(x2.sum), 0)) as Sum from tbl_user u left join tbl_x1 x1 on u.id = x1.user_id left join tbl_x2 g on u.id = x2.user_id left join tbl_y y on u.id = y.user_id group by u.id
,但我得到錯誤的結果。
而問題是什麼?你卡在哪裏?你有錯誤嗎? – 2015-04-05 12:58:26
該查詢不起作用。 – Garme 2015-04-05 13:17:58