我有运动队积分榜,有一个专栏“积分”(每场比赛* 3 +每场并列比赛)。
------------------------------------------
Name | Played | Won | Lost | Tied | Points
------------------------------------------
A | 3 | 2 | 1 | 0 | 6
B | 3 | 2 | 1 | 0 | 6
... (other rows not listed)
我按点分,然后直接匹配。 现在让我们说“B”赢了“A”,所以A应该先列出来。我该怎么做?
这是我的表格
CREATE TABLE IF NOT EXISTS `games` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`hometeam` int(11) NOT NULL,
`awayteam` int(11) NOT NULL,
`won` enum('home','away','tie') DEFAULT NULL,
`home_score` tinyint(3) unsigned DEFAULT NULL,
`away_score` tinyint(3) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我以这种方式得到我的排名表:
SELECT
tt.name
,(SELECT COUNT(*) FROM games tg WHERE (tt.id = tg.hometeam OR tt.id = tg.awayteam) AND tg.won IS NOT NULL) as `games_played`
,(SELECT COUNT(*) FROM games tg WHERE (tt.id = tg.hometeam AND tg.won = "home") OR (tt.id = tg.awayteam AND tg.won = "away")) as `games_won`
,(SELECT COUNT(*) FROM games tg WHERE (tt.id = tg.hometeam AND tg.won = "away") OR (tt.id = tg.awayteam AND tg.won = "home")) as `games_lost`
,(SELECT COUNT(*) FROM games tg WHERE (tt.id = tg.hometeam OR tt.id = tg.awayteam) AND tg.won = "tied") as `games_tied`
,(SELECT `games_won`*3 + `games_tied`) AS `points`
FROM
teams tt
ORDER BY `points` DESC