2010-12-31 8 views
0

スポーツのリストを含むスポーツと呼ばれるテーブル、特定のスポーツの季節を含む季節と競争のある競技特定のスポーツと季節。他のテーブルの2つのCOUNT()に条件がある場合のクエリ

スポーツのリストを印刷するには、季節や競争の度合いをそれぞれ1つの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 |    | 
+--------------------+------------------+------+-----+---------+----------------+ 

クエリの結果には、スポーツ。*、total_seasons(seasons.id_sport = sports.idのシーズンの合計)とtotal_competitions(competition.id_season = seasons.idとseasons.id_sport = sports.idの競合の合計)が含まれている必要があります。 。

ありがとうございます!

+0

に参加する参加する代わりに、内側の左に何かを確認してください...季節が同時に重なった...正しい複数のスポーツを持つことができますか? – DRapp

+0

@DRapp:各スポーツは季節を作ります。彼らはスポーツの間で共有されていません。したがって、サッカーは2010年から2011年のシーズンで、id = 1、id_sport = 1、サッカーシーズン2010-2011(id = 2、id_sport = 2)です。ありがとうございました! – ipalaus

答えて

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; 
+0

大丈夫ですが、競技のシーズンがない場合、TotalCompetitionsは1になります。シーズンがない場合、競争することはできません。どうすれば修正できますか?事前にありがとう、それはうまく動作しますが、この詳細のみ!明けましておめでとうございます! – ipalaus

+0

@Isern Palaus - 問題はアスタリスクの使用によって引き起こされます。アップデートバージョンが役立つはずです(またはそうではありません...) – ajreal

+0

それは機能しました! ;-) ありがとうございました! – ipalaus

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 
+0

Borealidありがとうございましたが、私はDRappにコメントしました。競技やシーズンがないスポーツの場合は、結果を得ることができません。どうすれば解決できますか?ありがとうございました! – ipalaus

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 
+0

DRappにありがとうございます。しかし、スポーツの中にはシーズンや競争がないと、その結果が得られないのはなぜか分かりません。どうすれば解決できますか?ありがとうございました! – ipalaus

+0

@Isern Palaus、私は@ajrealで提案されているように私のバージョンを左外部結合に変更しましたが、IFNULL()内でそのような一致がない場合はチェックします。結果セットにNULLの代わりにゼロの値を取得します。 – DRapp

関連する問題