[英]MySQL sum of counts of two queries
I am (very) new to MySQL. 我是MySQL的新手。 Forgive my lack of knowledge.... 原谅我缺乏知识。
I am working on a hockey stat database where I need to add all the assists and all the goals together to get "total points". 我正在曲棍球统计数据库上,我需要将所有辅助功能和所有目标加在一起以获得“总分”。 I have two queries already figured out, but I am not able to figure out how to sum the two. 我已经找到了两个查询,但是我无法弄清楚如何对两个查询求和。 Here are the queries: 以下是查询:
select player_id, count(*)
from(select * from 1st_assists
union
select * from 2nd_assists) as tem
join players on tem.fk_player_id=players.player_id
group by fk_player_id
order by count(*) desc
select player_id, count(*)
from goals_for
join shots_for on goals_for.fk_shot_for_id=shots_for.shot_for_id
join players on shots_for.fk_player_id=players.player_id
group by player_id
order by count(*) desc;
how do I combine these two queries into one and get the total of both counts? 如何将这两个查询合并为一个并获得两个计数的总和?
Here are the results of each of the queries 这是每个查询的结果
Total Assists
player_id count(*)
79 24
55 22
45 17
90 16
40 15
65 15
37 13
1 13
20 11
84 11
64 10
27 9
93 7
8 5
24 3
57 1
Goals
player_id count(*)
90 38
37 28
40 19
55 13
45 11
1 8
24 8
20 8
84 8
27 6
8 5
79 4
65 4
93 1
64 1
It is untested, but can you, please, try this: 它未经测试,但是可以,请尝试以下操作:
select p.player_first_name, p.player_last_name, (count1+count2) as total_count
from
(select player_id, count(*) count1
from(select * from 1st_assists
union
select * from 2nd_assists) as tem
join players on tem.fk_player_id=players.player_id
group by fk_player_id
order by count(*) desc) q1
left join
(select player_id, count(*) count2
from goals_for
join shots_for on goals_for.fk_shot_for_id=shots_for.shot_for_id
join players on shots_for.fk_player_id=players.player_id
group by player_id) q2
ON q1.player_id=q2.player_id
left join player p ON q1.player_id=p.player_id
order by (count1+count2) desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.