MySQL查询2 COUNT()的其他表与where条件

时间:2010-12-31 14:59:23

标签: mysql

我有一张名为sports的表,其中包含一系列运动列表,其他称为季节,包含特定运动的季节以及具有特定运动和赛季比赛的比赛。

我需要一个MySQL查询来打印体育列表,其中每个季节和比赛有多少。我的表结构:

运动

+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| id                 | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name               | varchar(32)      | NO   |     | NULL    |                |
| slug               | varchar(45)      | NO   |     | NULL    |                |
| description        | varchar(128)     | NO   |     | NULL    |                |
+--------------------+------------------+------+-----+---------+----------------+

季节

+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| id                 | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| id_sport           | int(10) unsigned | NO   | MUL | NULL    |                |
| name               | varchar(32)      | NO   |     | NULL    |                |
| slug               | varchar(32)      | NO   |     | NULL    |                |
+--------------------+------------------+------+-----+---------+----------------+

比赛

+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| id                 | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| id_season          | int(10) unsigned | NO   | MUL | NULL    |                |
| name               | varchar(32)      | NO   |     | NULL    |                |
| slug               | varchar(64)      | NO   |     | NULL    |                |
| description        | varchar(128)     | YES  |     | NULL    |                |
+--------------------+------------------+------+-----+---------+----------------+

我的查询结果需要包含:sports。*,total_seasons(Seasons of season.id_sport = sports.id)和total_competitions(competitions.id_season = seasons.id和seasons.id_sport = sports的比赛总和) .ID)。

提前谢谢!

3 个答案:

答案 0 :(得分:1)

SELECT sports.*,
    COUNT(seasons.id) AS total_seasons,
    COUNT(competitions.id) AS total_competitions
    FROM sports, seasons, competitions
    WHERE sports.id=id_sport AND seasons.id=id_season
    GROUP BY sports.id

答案 1 :(得分:1)

select
      sports.id,
      sports.name,
      sports.slug,
      sports.description,
      count( distinct seasons.id ) as DistinctSeasons,
      count( * ) as TotalCompetitions
   from 
      sports 
         left outer join Seasons
            on sports.id = seasons.id_sport,
         left outer join Competitions
            on seasons.id = Competitions.id_season
   group by
      sports.id

答案 2 :(得分:1)

使用左连接而不是内连接

select
  sports.id,
  sports.name,
  sports.slug,
  sports.description,
  ifnull(count( distinct seasons.id ), 0) as DistinctSeasons,
  ifnull(count( distinct competitions.id ), 0) as TotalCompetitions
from 
  sports
  left join seasons on sports.id=seasons.id_sport
  left join competitions on seasons.id = competitions.id_season
group by
  sports.id;
相关问题