计算具有内部连接表的行

emu*_*tol 8 mysql join

我有3张桌子:

球员:

    mysql> SELECT * FROM players;
+-----------+---------+----------------------+----------------------+-----------------+------------------------------+-----------------------+---------------------+
| player_id | team_id | player_name          | player_jersey_number | player_position | player_email                 | player_contact_number | player_timestamp    |
+-----------+---------+----------------------+----------------------+-----------------+------------------------------+-----------------------+---------------------+
|         1 |       4 | Popoy Alfonso        |                    2 |                 | popoyalfonso@gmail.com       | 09263453234           | 2015-08-05 00:48:10 |
|         2 |       4 | Karlo Ripas          |                   10 |                 | karloripas@yahoo.com         | 09212354324           | 2015-08-05 00:50:03 |
|         3 |       4 | VHaughn Von          |                   32 |                 | von@outlook.com              | 09361234565           | 2015-08-05 00:51:00 |
|         4 |       4 | Lordie Zalbahe       |                   23 |                 | lordiezalbahe@gmail.com      | 09391222334           | 2015-08-05 00:52:42 |
|         5 |       4 | Jigs Selda           |                    8 |                 | jigsselda@gmail.com          | 09325566323           | 2015-08-05 00:53:36 |
|         6 |       4 | Rhan Garniel         |                    3 |                 | rhangarniel@ymail.com        | 09129503400           | 2015-08-05 00:54:20 |
|         7 |       5 | Johnritz Rodriguez   |                   11 |                 | johnritz@gmail.com           | 09231112346           | 2015-08-05 00:56:02 |
|         8 |       5 | Garret Van Sarmiento |                    7 |                 | garretvansarmiento@gmail.com | 09264565600           | 2015-08-05 00:56:53 |
|         9 |       5 | Lester Selda Lineses |                   12 |                 | lesterlineses@yahoo.com      | 09068746354           | 2015-08-05 00:57:47 |
|        10 |       5 | Laurence Lineses     |                   44 |                 | laurencelineses@yahoo.com    | 09847354672           | 2015-08-05 00:59:33 |
|        11 |       5 | Xandrix Buendia      |                    1 |                 | xandrixbuendia@yahoo.com     | 09234665590           | 2015-08-05 01:00:12 |
|        12 |       5 | Betoyskie Limpiada   |                   45 |                 | betoyskie@outlook.com        | 09213456667           | 2015-08-05 01:01:15 |
+-----------+---------+----------------------+----------------------+-----------------+------------------------------+-----------------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

团队:

    mysql> SELECT * FROM teams;
+---------+-----------+----------------------+---------------------+
| team_id | season_id | team_name            | team_timestamp      |
+---------+-----------+----------------------+---------------------+
|       4 |         1 | Quiapo A             | 2015-08-05 00:30:13 |
|       5 |         1 | Quiapo B             | 2015-08-05 00:30:25 |
|       6 |         1 | Balik-Balik Warriors | 2015-08-05 00:31:13 |
|       7 |         1 | Adamson Falcons      | 2015-08-05 00:31:42 |
|       8 |         1 | Pasay Flooders       | 2015-08-05 00:32:04 |
|       9 |         1 | Marina Dragons       | 2015-08-05 00:32:22 |
|      10 |         1 | MDC Archers          | 2015-08-05 00:33:12 |
|      11 |         2 | Quiapo A             | 2015-08-05 00:34:25 |
|      12 |         2 | Quiapo B             | 2015-08-05 00:34:38 |
|      13 |         2 | Marikina Eagels      | 2015-08-05 00:35:11 |
|      14 |         2 | TIP Steallers        | 2015-08-05 00:35:32 |
|      15 |         2 | Gasan Blue Eagles    | 2015-08-05 00:36:12 |
+---------+-----------+----------------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

季节:

mysql> SELECT * FROM seasons;
+-----------+-------------+----------------------+---------------------+
| season_id | season_name | season_event_name    | season_timestamp    |
+-----------+-------------+----------------------+---------------------+
|         1 | Season 1    | Summer Games         | 2015-08-05 00:23:15 |
|         2 | Season 2    | Aniversary Sportfest | 2015-08-05 00:25:10 |
+-----------+-------------+----------------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

我目前正在处理此查询,但结果不正确。

SELECT 
    teams.team_name,
    (
        SELECT COUNT(*) 
        FROM teams 
        INNER JOIN players 
            ON teams.team_id = players.team_id
    ) as num_of_players, 
    teams.team_timestamp
FROM teams 
INNER JOIN seasons 
    ON seasons.season_id = teams.season_id 
GROUP BY teams.team_name;
Run Code Online (Sandbox Code Playgroud)

输出:

+----------------------+----------------+---------------------+
| team_name            | num_of_players | team_timestamp      |
+----------------------+----------------+---------------------+
| Adamson Falcons      |             12 | 2015-08-05 00:31:42 |
| Balik-Balik Warriors |             12 | 2015-08-05 00:31:13 |
| Gasan Blue Eagles    |             12 | 2015-08-05 00:36:12 |
| Marikina Eagels      |             12 | 2015-08-05 00:35:11 |
| Marina Dragons       |             12 | 2015-08-05 00:32:22 |
| MDC Archers          |             12 | 2015-08-05 00:33:12 |
| Pasay Flooders       |             12 | 2015-08-05 00:32:04 |
| Quiapo A             |             12 | 2015-08-05 00:30:13 |
| Quiapo B             |             12 | 2015-08-05 00:30:25 |
| TIP Steallers        |             12 | 2015-08-05 00:35:32 |
+----------------------+----------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

我想要的结果是这样的:

+----------------------+----------------+---------------------+
| team_name            | num_of_players | team_timestamp      |
+----------------------+----------------+---------------------+
| Adamson Falcons      |              0 | 2015-08-05 00:31:42 |
| Balik-Balik Warriors |              0 | 2015-08-05 00:31:13 |
| Gasan Blue Eagles    |              0 | 2015-08-05 00:36:12 |
| Marikina Eagels      |              0 | 2015-08-05 00:35:11 |
| Marina Dragons       |              0 | 2015-08-05 00:32:22 |
| MDC Archers          |              0 | 2015-08-05 00:33:12 |
| Pasay Flooders       |              0 | 2015-08-05 00:32:04 |
| Quiapo A             |              6 | 2015-08-05 00:30:13 |
| Quiapo B             |              6 | 2015-08-05 00:30:25 |
| TIP Steallers        |              0 | 2015-08-05 00:35:32 |
+----------------------+----------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

Vér*_*ace 4

完全重新排列的答案。

我已向玩家和球队表中添加了一些数据,以使答案更加通用 - 请参阅帖子底部,了解此答案中使用的所有 DDL ( CREATE TABLE tab_name...) 和 DML ( INSERT INTO tab_name VALUES...)。我还创建了季节表(与 OP 相同 - 即您的)原始数据。

顺便说一句,欢迎来到论坛。但您确实应该给我们 DDL 和 DML。查看游览以及“如何帮助我们帮助您”博客 - 均位于页面左下角。但是,我感兴趣并自己做了,但是如果你提供 DDL 和 DML,你会得到更多的人帮助你。

创建并加载表后,我运行了以下 SQL。

SELECT t1.team_name,
       IFNULL(t2.num_players, 0) AS strength,
       t1.team_timestamp
FROM team t1
LEFT OUTER JOIN 
    (SELECT team_id, COUNT(team_id) AS num_players 
     FROM player 
     GROUP BY team_id
    ) t2
ON t1.team_id = t2.team_id
-- GROUP BY t1.team_id, t1.team_name, t1.season_id  -- **NOTE** - see discussion below
ORDER BY strength DESC, team_name ASC;  
Run Code Online (Sandbox Code Playgroud)

结果并不完全是你想要的,但也很接近。

+----------------------+----------+---------------------+
| team_name            | strength | team_timestamp      |
+----------------------+----------+---------------------+
| Quiapo B             |        7 | 2015-08-05 00:30:25 |
| Quiapo B             |        7 | 2015-08-05 00:30:25 |
| Quiapo A             |        6 | 2015-08-05 00:30:13 |
| Quiapo A             |        6 | 2015-08-05 00:30:13 |
| Adamson Falcons      |        0 | 2015-08-05 00:31:42 |
| Balik-Balik Warriors |        0 | 2015-08-05 00:31:13 |
| Gasan Blue Eagles    |        0 | 2015-08-05 00:36:12 |
| Marikina Eagels      |        0 | 2015-08-05 00:35:11 |
| Marina Dragons       |        0 | 2015-08-05 00:32:22 |
| MDC Archers          |        0 | 2015-08-05 00:33:12 |
| Pasay Flooders       |        0 | 2015-08-05 00:32:04 |
| TIP Steallers        |        0 | 2015-08-05 00:35:32 |
+----------------------+----------+---------------------+
Run Code Online (Sandbox Code Playgroud)

运行 oNare 的查询给出(!)

+-----------+----------------+---------------------+
| team_name | num_of_players | team_timestamp      |
+-----------+----------------+---------------------+
| Quiapo A  |             26 | 2015-08-05 00:30:13 |
+-----------+----------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

请注意,26 = 2 * (6 + 7)。但是,如果sql_mode设置为STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,此查询将失败并显示消息

ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
Run Code Online (Sandbox Code Playgroud)

然而,对 oNare 的查询进行轻微修改会得到与我的第一个查询相同的结果(sql_mode 未设置为ONLY_FULL_GROUP_BY)。

SELECT 
team.team_name, 
COUNT(player.player_id) as num_of_players,
team.team_timestamp
FROM team 
LEFT JOIN player ON (player.team_id = team.team_id)
LEFT JOIN seasons ON (seasons.season_id = team.season_id) 
GROUP BY team.team_name, team.season_id, team.team_timestamp -- **ADDED**
ORDER BY num_of_players DESC, team.team_name ASC;
Run Code Online (Sandbox Code Playgroud)

添加 GROUP BY 给出了正确的答案。GROUP BY有趣的是,如果该行被注释掉,PostgreSQL 将抛出错误。GROUP BY如果我不在该行之前添加一行,我的查询也将无法在 PostgreSQL 上运行ORDER BY

我认为作为一般答案,您可以接受我们的任何一个答案作为您的问题的正确答案(带有GROUP BY) - 如果没有表格中的更多信息,就不可能获得您想要的结果,但请参见下文。

顺便说一句,ONLY_FULL_GROUP_BY这将是5.7 的默认模式,所以最好现在就获取它!

我认为你的表结构需要一些修改。你应该拥有不同赛季的球员(这是有道理的,人们会转会)。对于同一支球队在不同赛季中应该有相同的ids - 否则你将如何汇总多个赛季的统计数据(进球/得分/赢得/输掉的比赛)?

表 DDL 和 DML - 结构和内容。

CREATE TABLE player (player_id INT, team_id INT, player_name VARCHAR(25));
CREATE TABLE team( team_id INT, season_id INT, team_name VARCHAR(25), team_timestamp TIMESTAMP);
CREATE TABLE season (season_id INT, season_name VARCHAR(25));

INSERT INTO player VALUES (1, 4, 'Popoy Alfonso'); 
INSERT INTO player VALUES (2, 4, 'Karlo Ripas');
INSERT INTO player VALUES (3, 4, 'VHaughn Von');
INSERT INTO player VALUES (4, 4, 'Lordie Zalbahe');
INSERT INTO player VALUES (5, 4, 'Jigs Selda'          );
INSERT INTO player VALUES (6, 4, 'Rhan Garniel'        );
INSERT INTO player VALUES (7, 5, 'Johnritz Rodriguez'  );
INSERT INTO player VALUES (8, 5, 'Garret Van Sarmiento');
INSERT INTO player VALUES (9, 5, 'Lester Selda Lineses');
INSERT INTO player VALUES (10, 5,  'Laurence Lineses'    );
INSERT INTO player VALUES (11, 5,  'Xandrix Buendia'      );
INSERT INTO player VALUES (12, 5, 'Betoyskie Limpiada'   );
INSERT INTO player VALUES (12, 5, 'Donald Duck'   );  -- Added Donald Duck!

-- I inserted extra records into the player table as below.

INSERT INTO player VALUES (1, 11, 'Popoy Alfonso'); 
INSERT INTO player VALUES (2, 11, 'Karlo Ripas');
INSERT INTO player VALUES (3, 11, 'VHaughn Von');
INSERT INTO player VALUES (11, 11, 'Lordie Zalbahe');
INSERT INTO player VALUES (5, 11, 'Jigs Selda'          );
INSERT INTO player VALUES (6, 11, 'Rhan Garniel'        );
INSERT INTO player VALUES (7, 12, 'Johnritz Rodriguez'  );
INSERT INTO player VALUES (8, 12, 'Garret Van Sarmiento');
INSERT INTO player VALUES (9, 12, 'Lester Selda Lineses');
INSERT INTO player VALUES (10, 12,  'Laurence Lineses'    );
INSERT INTO player VALUES (11, 12,  'Xandrix Buendia'      );
INSERT INTO player VALUES (12, 12, 'Betoyskie Limpiada'   );
Run Code Online (Sandbox Code Playgroud)

对于球队表,我在第 1 季和第 2 季中给 QuiradoA 和 QuiradoB 相同- 对我来说,仅仅因为赛季变化而对同一支球队id使用不同的 s 是没有意义的。id我还TIMESTAMP为 QuiradoA 和 B保留了相同的内容。

INSERT INTO team VALUES (   4 ,         1 , 'Quiapo A'             , '2015-08-05 00:30:13');
INSERT INTO team VALUES (   5 ,         1 , 'Quiapo B'             , '2015-08-05 00:30:25');
INSERT INTO team VALUES (   6 ,         1 , 'Balik-Balik Warriors' , '2015-08-05 00:31:13');
INSERT INTO team VALUES (   7 ,         1 , 'Adamson Falcons'      , '2015-08-05 00:31:42');
INSERT INTO team VALUES (   8 ,         1 , 'Pasay Flooders'       , '2015-08-05 00:32:04');
INSERT INTO team VALUES (   9 ,         1 , 'Marina Dragons'       , '2015-08-05 00:32:22');
INSERT INTO team VALUES (  10 ,         1 , 'MDC Archers'          , '2015-08-05 00:33:12');
INSERT INTO team VALUES (   4 ,         2 , 'Quiapo A'             , '2015-08-05 00:30:13'); -- **NOTE** `id` and `TIMESTAMP` for both A & B.
INSERT INTO team VALUES (   5 ,         2 , 'Quiapo B'             , '2015-08-05 00:30:25');
INSERT INTO team VALUES (  13 ,         2 , 'Marikina Eagels'      , '2015-08-05 00:35:11');
INSERT INTO team VALUES (  14 ,         2 , 'TIP Steallers'        , '2015-08-05 00:35:32');
INSERT INTO team VALUES (  15 ,         2 , 'Gasan Blue Eagles'    , '2015-08-05 00:36:12');



INSERT INTO season VALUES(1, 'Season 1');
INSERT INTO season VALUES(2, 'Season 2');
Run Code Online (Sandbox Code Playgroud)