子查询生成空集

时间:2018-03-26 07:29:15

标签: sql subquery

我正在尝试提供一个活跃的四分卫名单,这些四分卫为本赛季有20次以上麻袋的球队效力。

我有两个表中的信息,其中一个是一个视图(v_active_quarterbacks),它显示哪些四分卫是活动的,另一个是表team_game_stats。

我创建了一个命令,列出了有20多个麻袋的团队。

SELECT SUM(sacks)
FROM team_game_stats
GROUP BY team_code
HAVING SUM(sacks) > 20;

我现在需要将它连接到v_active_quarterbacks,以便我可以获得一个列表。我尝试了以下但它只提供了一个空集。

SELECT player_code
FROM v_active_quaterbacks
    INNER JOIN team_game_stats ON v_active_quaterbacks.team_code = team_game_stats.team_code
WHERE sacks IN (SELECT SUM(sacks)
                FROM team_game_stats
                GROUP BY team_code
                HAVING SUM(sacks) > 20);

以下是视图说明:

+-----------------------+-------------+------+-----+---------+-------+
| Field                 | Type        | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+-------+
| player_code           | int(11)     | NO   |     | NULL    |       |
| first_name            | varchar(30) | YES  |     | NULL    |       |
| last_name             | varchar(30) | YES  |     | NULL    |       |
| team_code             | int(11)     | YES  |     | NULL    |       |
| uniform_number        | varchar(3)  | YES  |     | NULL    |       |
| passes_player_code    | int(11)     | YES  |     | NULL    |       |
| COUNT(passes.attempt) | bigint(21)  | NO   |     | 0       |       |
+-----------------------+-------------+------+-----+---------+-------+

此时我感到困惑和困惑。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以在subselect上使用内部联接来获取team_code和sum

SELECT player_code , T.sum_sacks
FROM v_active_quaterbacks  
INNER JOIN team_game_stats ON v_active_quaterbacks.team_code = team_game_stats.team_code 
INNER JOIN (  
      SELECT team_code, SUM(sacks)  sum_sacks
      FROM team_game_stats 
      GROUP BY team_code 
      HAVING SUM(sacks) > 20
) T on T.team_code = v_active_quaterbacks.team_code