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 查询必须是什么样子。有什么建议么?

提前谢谢

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 回答