从多个表中选择列的总和,并按ID分组

时间:2018-09-28 10:50:26

标签: php mysql sql

我有以下数据

purchase_rec_a

+----------+------------+----------------+
| user     | date       | total          |
+----------+------------+----------------+
| 1510     | 2018-08-08 | 5              |
| 1510     | 2018-09-12 | 10             |
| 1510     | 2018-09-19 | 15             |
+----------+------------+----------------+

purchase_rec_b

+----------+------------+----------------+
| user     | date       | total          |
+----------+------------+----------------+
| 1510     | 2018-05-08 | 10             |
| 1510     | 2018-07-02 | 15             |
+----------+------------+----------------+

我可以按照sql左联接两个表

SELECT u.id, a.date, a.total AS total_a, b.date, b.total AS 
total_b
FROM users AS u
LEFT JOIN purchase_rec_a AS a ON u.id = a.user
LEFT JOIN purchase_rec_b AS b ON u.id = b.user
WHERE u.id =1510

我得到以下内容

+-------+------------+---------+------------+---------+
| id    | date       | total_a | date       | total_b |
+-------+------------+---------+------------+---------+
| 1510  | 2018-08-08 | 5       | 2018-05-08 | 10      |
| 1510  | 2018-08-08 | 5       | 2018-07-02 | 15      |
| 1510  | 2018-09-12 | 10      | 2018-05-08 | 10      |
| 1510  | 2018-09-12 | 10      | 2018-07-02 | 15      |
| 1510  | 2018-09-19 | 15      | 2018-05-08 | 10      |
| 1510  | 2018-09-19 | 15      | 2018-07-02 | 15      |
+-------+------------+---------+------------+---------+

所以我在这里要做的是按照不同的方式将它们按total_a和total_b分组,并按用户分组,所以我尝试了

SELECT u.id, a.date, SUM( a.total ) AS total_a, 
b.date, SUM( b.total ) AS total_b
FROM users AS u
LEFT JOIN purchase_rec_a AS a ON u.id = a.user
LEFT JOIN purchase_rec_b AS b ON u.id = b.user
WHERE u.id =1510

结果是

+------+------------+---------+------------+---------+
| id   | date       | total_a | date       | total_b |
+------+------------+---------+------------+---------+
| 1510 | 2018-08-08 | 60      | 2018-05-08 | 75      |
+------+------------+---------+------------+---------+

但是这里的问题是一些数据被重复并被添加为总数

我的预期结果低于

+------+------------+---------+------------+---------+
| id   | date       | total_a | date       | total_b |
+------+------------+---------+------------+---------+
| 1510 | 2018-08-08 | 30      | 2018-05-08 | 25      |
+------+------------+---------+------------+---------+

我可以通过多个SQL获得以上结果,但是我希望在单个SQL中获得它,这可能吗?

2 个答案:

答案 0 :(得分:1)

如果要“并排放置”数据,建议使用union allgroup by

select user, date, sum(a_total) as a_total, sum(b_total) as b_total
from ((select user, date, total as a_total, 0 as b_total
       from purchase_rec_a
      ) union all
      (select user, date, 0 as a_total, total as b_total
       from purchase_rec_b
      )
     ) ab
group by user, user_date;

如果您不想在结果中添加日期,则可以使用相同的结构,只需更改外部查询即可:

select user, sum(a_total) as a_total, sum(b_total) as b_total
from ((select user, date, total as a_total, 0 as b_total
       from purchase_rec_a
      ) union all
      (select user, date, 0 as a_total, total as b_total
       from purchase_rec_b
      )
     ) ab
group by user;

答案 1 :(得分:0)

您可以使用子查询联接两个表

 select t1.user,a_total,b_total from

  (
select user, sum(total) a_total from purchase_rec_a group by user
  )
  join 

  (select user, sum(total) b_total from purchase_rec_b group by user
   ) t2 on t1.user=t2.user