拥有以下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查询必须如何。有什么建议?
提前谢谢
答案 0 :(得分: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
答案 1 :(得分: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