我是(非常)新的MySQL。原谅我缺乏知识......
我正在制作一个曲棍球统计数据库,我需要将所有助攻和所有目标加在一起以获得“总积分”。我已经发现了两个问题,但我无法弄清楚如何将两者相加。 以下是查询:
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;
如何将这两个查询合并为一个并获得两个计数的总和?
以下是每个查询的结果
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
答案 0 :(得分:0)
这是未经测试的,但请你试试这个:
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;