表a(作者表)
author_id author_name
Table b (Post Table)
post_id author_id
Table c (Earning Table)
post_id (post id is not unique) post_earning
I wanted to generate a report consist of earnings per author.
author_id author_name total_earning (sum of earnings of all the posts by author)
The SQL Query used:
SELECT
a.author_id,
a.author_name,
sum(post_earnings) as total_earnings
FROM TableA a
Inner Join TableB b on b.author_id = a.author_id
Inner Join TableC c on c.post_id = b.post_id
Group By
a.author_id,
a.author_name
我得到的结果是这样的:
ID user_login total_earnings 2 Redstar 13.99 7 Redleaf 980.18 10 topnhotnews 80.43 11 zmmishad 39.27 13 rashel 1248.34 14 coolsaint 1.66 16 hotnazmul 9.83 17 rubel 0.14 21 mahfuz1986 1.09 48 ripon 12.96 60 KHK 27.81
总收入的总和实际上是2863.22。但是,如果我将结果表的所有值相加,我会得到 2415。问题出在哪里?可以从第一个评论中的链接下载使用的示例表。
最佳答案
SELECT
a.author_id,
a.author_name,
sum(post_earnings) as total_earnings
FROM TableA a
Inner Join TableB b on b.author_id = a.author_id
Inner Join TableC c on c.post_id = b.post_id
Group By
a.author_id,
a.author_name
关于mysql - 我有 3 个表,如何将它们连接起来生成一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3956027/