[英]Subquery to SUM info not included in table result from JOIN MySQL
So I feel like this has probably been answered somewhere already, but I've scoured the internet all night and continue to come up empty handed. 因此,我觉得这可能已经在某个地方得到了解答,但是我整夜都在搜寻互联网并继续空手而归。 I have 3 tables in a MySQL database and I'm using a JOIN statement on them. 我在MySQL数据库中有3个表,并且正在对它们使用JOIN语句。 I'm filtering the results by sport (ie, only those with sport equal to NFL). 我按运动过滤结果(即,只有那些运动等于NFL的结果)。
The problem is I want to create a column on the filtered results that sums the amount of times stats in other sports show up as well. 问题是我想在过滤后的结果上创建一列,以汇总其他体育项目中统计信息显示的时间。 But following the WHERE Sport.type = 'NFL'
that becomes impossible because I've filtered out all those instances. 但是遵循WHERE Sport.type = 'NFL'
成为不可能,因为我已经过滤掉了所有这些实例。 I'm now thinking I need something along the lines of a subquery, but I'm not sure how to organize it so that I can draw query results that list each player_id
only once and then allow me to tabulate columns of data referencing any information related to that player_id
throughout the database, even if it's been filtered through the JOIN
statement. 我现在想我需要一些子查询,但是我不确定如何组织它,以便可以绘制仅列出每个player_id
一次的查询结果,然后允许我将引用任何信息的数据列成表格与整个数据库中的那个player_id
有关,即使已通过JOIN
语句过滤了它也是如此。 I feel like this should be so easy and I'm just missing something. 我觉得这应该很简单,我只是想念一些东西。
Really simple code example is below. 真正简单的代码示例如下。 Can someone please help? 有人可以帮忙吗? Thank you in advance! 先感谢您!
SELECT
Player.player_id,
Player.contact,
SUM(CASE WHEN Sport.type != 'NFL' THEN Info.Minutes ELSE 0 END)
FROM
Player
JOIN Info ON Player.player_id = Info.player_id
JOIN Sport ON Sport.game_id = Info.game_id
WHERE Sport.type = 'NFL'
GROUP BY Player.player_id);
SELECT
Player.player_id,
Player.contact,
0 AS stats
FROM
Player
JOIN Info ON Player.player_id = Info.player_id
JOIN Sport ON Sport.game_id = Info.game_id
WHERE Sport.type = 'NFL'
GROUP BY Player.player_id
UNION
SELECT
Player.player_id,
Player.contact,
SUM(Minutes) AS stats
FROM
Player
JOIN Info ON Player.player_id = Info.player_id
JOIN Sport ON Sport.game_id = Info.game_id
WHERE Sport.type != 'NFL'
GROUP BY Player.player_id;
UPDATE 更新
To get unique players but different stats use below query. 要获得独特的玩家但统计数据不同,请使用以下查询。
Eg 例如
If a player falls under both NFL and non NFL, it will return
NFL_only
as1
andAll_other_sports
asSum of minutes
. 如果玩家同时处于NFL和非NFL之下,则它将返回NFL_only
为1
以及All_other_sports
为Sum of minutes
All_other_sports
。If a player falls under NFL only, it will return NFL_only
as
1and
All_other_sportsas
0`. 如果玩家仅属于NFL,则它将返回NFL_onlyas
1and
All_other_sportsas
0`。If a player falls under Other sports only, it will return NFL_only
as
0and
All_other_sportsas
Sum of minutes`. 如果玩家仅属于“其他运动”,则它将返回NFL_onlyas
0and
All_other_sportsas
分钟总和。
SELECT z.player_id, z.contact, SUM(z.NFL_only) AS NFL_only, SUM(z.All_other_sports) AS All_other_sports
FROM (
SELECT
Player.player_id,
Player.contact,
1 AS NFL_only,
0 AS All_other_sports
FROM
Player
JOIN Info ON Player.player_id = Info.player_id
JOIN Sport ON Sport.game_id = Info.game_id
WHERE Sport.type = 'NFL'
GROUP BY Player.player_id
UNION
SELECT
Player.player_id,
Player.contact,
0 AS NFL_only,
SUM(Minutes) AS All_other_sports
FROM
Player
JOIN Info ON Player.player_id = Info.player_id
JOIN Sport ON Sport.game_id = Info.game_id
WHERE Sport.type != 'NFL'
GROUP BY Player.player_id;
) z
GROUP BY z.player_id;
Update 2 更新2
As per your clear requirement, If one of my original tables has a column indicating the sport (ie, NFL, MLB, NBA), what I want is a list of the players who have played NFL at least once. 根据您的明确要求,如果我的一张原始桌子上有一栏指示这项运动(即NFL,MLB,NBA),那么我想要的是至少曾经参加过NFL的球员名单。 Then, if they've played the other sports I'd like to know how many times they've played them. 然后,如果他们参加了其他运动,我想知道他们参加了多少次。 The problem is if I filter only those rows with NFL I can no longer count the rows that had MLB and NBA because I've removed them with the JOIN. 问题是,如果我仅使用NFL过滤这些行,我将无法再计算具有MLB和NBA的行,因为我已使用JOIN删除了这些行。
SELECT
Player.player_id,
Player.contact,
SUM(CASE WHEN Sport.type != 'NFL' THEN Info.Minutes ELSE 0 END)
FROM
Player
JOIN Info ON Player.player_id = Info.player_id
JOIN Sport ON Sport.game_id = Info.game_id
WHERE Player.player_id IN (
SELECT
Player.player_id,
FROM
Player
JOIN Info ON Player.player_id = Info.player_id
JOIN Sport ON Sport.game_id = Info.game_id
WHERE Sport.type = 'NFL'
GROUP BY Player.player_id
)
GROUP BY Player.player_id;
The subquery will return only NFL players. 子查询将仅返回NFL球员。 The main query will filter only NFL players and return expected output. 主要查询将仅过滤NFL播放器并返回预期输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.