从两个表中拉出的嵌套SQL语句

时间:2011-11-07 19:48:36

标签: php mysql html count

我试图在试图从两个表中提取数据的SQL语句中嵌套,计算来自player_id的行,来自1st_assist和2nd_assist的行,相应地显示在3列中。

这是我的代码。

SELECT 

     last_name, 

     (SELECT COUNT (*) FROM goals WHERE player_id = 4 OR player_id = 5) 
     AS player_goals, 

     (SELECT COUNT (*) FROM goals WHERE 1st_assist = 4 OR 1st_assist = 5) 
     AS 1st_assists, 

     (SELECT COUNT (*) FROM goals WHERE 2nd_assist = 4 OR 2nd_assist = 5)
     AS 2nd_assists 

FROM people 

WHERE player_id = 4 OR player_id = 5 

GROUP BY player_id

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT 
    p.last_name,
    COUNT(DISTINCT g.id) AS player_goals,
    COUNT(DISTINCT a1.id) AS 1st_assists,
    COUNT(DISTINCT a2.id) AS 2nd_assists
FROM
    people p
    LEFT JOIN goals g ON (g.player_id = p.player_id)
    LEFT JOIN goals a1 ON (a1.1st_assist = p.player_id)
    LEFT JOIN goals a2 ON (a2.2nd_assist = p.player_id)
WHERE
    p.player_id = 4 OR p.player_id = 5
GROUP BY p.player_id


测试:

mysql> select * from people;
+-----------+-----------+------------+
| player_id | last_name | first_name |
+-----------+-----------+------------+
|         4 | Francis   | Jeff       |
|         5 | Doe       | John       |
|         6 | Barratt   | Glen       |
+-----------+-----------+------------+
3 rows in set (0.00 sec)

mysql> select * from goals;
+----+-----------+------------+------------+---------------------+
| id | player_id | 1st_assist | 2nd_assist | when_made           |
+----+-----------+------------+------------+---------------------+
|  1 |         4 |          6 |          5 | 2011-11-07 15:05:31 |
|  2 |         4 |          5 |       NULL | 2011-11-07 15:05:37 |
|  3 |         5 |          4 |          6 | 2011-11-07 15:05:45 |
|  4 |         6 |          4 |          5 | 2011-11-07 15:05:52 |
|  5 |         4 |          6 |       NULL | 2011-11-07 15:06:05 |
|  6 |         5 |       NULL |       NULL | 2011-11-07 15:06:15 |
|  7 |         6 |          4 |       NULL | 2011-11-07 15:06:21 |
|  8 |         5 |          6 |       NULL | 2011-11-07 15:06:31 |
|  9 |         4 |       NULL |       NULL | 2011-11-07 15:15:21 |
| 10 |         4 |          6 |       NULL | 2011-11-07 15:15:27 |
| 11 |         6 |          5 |          4 | 2011-11-07 15:15:38 |
+----+-----------+------------+------------+---------------------+
11 rows in set (0.00 sec)

mysql> SELECT p.last_name, COUNT(DISTINCT g.id) AS player_goals, COUNT(DISTINCT a1.id) AS 1st_assists, COUNT(DISTINCT a2.id) AS 2nd_assists FROM people p LEFT JOIN goals g ON (g.player_id = p.player_id) LEFT JOIN goals a1 ON (a1.1st_assist = p.player_id) LEFT JOIN goals a2 ON (a2.2nd_assist = p.player_id) WHERE p.player_id = 4 OR p.player_id = 5 GROUP BY p.player_id;
+-----------+--------------+-------------+-------------+
| last_name | player_goals | 1st_assists | 2nd_assists |
+-----------+--------------+-------------+-------------+
| Francis   |            5 |           3 |           1 |
| Doe       |            3 |           2 |           2 |
+-----------+--------------+-------------+-------------+
2 rows in set (0.00 sec)