mysql - 我有 3 个表,如何将它们连接起来生成一个表?

标签 mysql

表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/

相关文章:

python - 来自外部调用的MySQL存储过程权限

PHP MYSQL 寻找一种对具有多个详细信息和总计的行进行分组的方法

MySQL:str_to_date多种格式

mysql - 我需要帮助编写家谱的 MySQL INSERT 查询

php - 如何在 Multi-Tenancy SaaS 产品中容纳每个用户的业务逻辑?

MySQL - 仅当列有值时才选择列

mysql - 从数据库中获取值

mysql - mysql中的比例WHERE语句应用于DATETIME字段

mysql - 从 MySQL 字段中删除字符串?

php - 查询未按预期显示帐户名称和公司名称