0

次のgames表があります

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `content` text COLLATE utf8_unicode_ci,
  `team1ID` int(7) unsigned DEFAULT NULL,
  `team2ID` int(7) unsigned DEFAULT NULL,
  `championshipID` int(10) unsigned DEFAULT NULL,
   `date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `FK_Event_team1ID_Team_id` FOREIGN KEY (`team1ID`) REFERENCES `Team` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `FK_Event_team2ID_Team_id` FOREIGN KEY (`team2ID`) REFERENCES `Team` (`id`) ON DELETE SET NULL ON UPDATE CASCADE

team必要なのは、ゲーム数 (残りのゲーム数) で並べられたテーブルからチーム名のリストを取得することです。gamesテーブルにあるゲームに関するすべての詳細

条件を説明します:

  • games.championshipID=1
  • リストは降順 (最大ゲーム数から最小ゲーム数の順) にする必要があります
  • gamesゲーム数は、次を検索してテーブルで計算する必要がありますgames.team1ID, games.team2ID
  • およびgames.date> 現在のタイムスタンプ

まあ結果はそのように見えるはずです

 team name | games left
   teamWithId5  | 68 (row count from games table)
   teamWithId250| 50 
   teamWithId250 | 4

SQL クエリがどのように見える必要があるかわかりません。助言がありますか?

事前にThx

4

2 に答える 2

0
select concat('teamwithid', temp_table.id), count(*) as games
from
(
select teamid1 as id from games where games.date>curdate()
union all
select teamid2 from games games.date>curdate()
) as temp_table
group by 1 order by 2 desc
于 2013-09-17T09:34:48.740 に答える
0
select t.Name,
       count(*) as gameCount
from   (select team1ID as id
        from   games
        where  championshipId = 1 and
               date > now()
        union all
        select team2ID as id
        from   games
        where  championshipId = 1 and
               date > now()) allGames 
        join team t on allGames.id = t.Id
group by t.Name
order by gameCount desc
于 2013-09-17T09:38:09.080 に答える