我在足球联赛中有一个表'游戏'如下:
date home_team_id away_team_id home_score away_score
- 1 2 6 21
- 3 1 7 19
我无法弄清楚如何动态生成Wins排序的团队ID列表(如果可能则点数)?
-
我有这个查询,当我有$ team_id时工作正常但是因为我一次只能做一个团队,而且不允许在查询级别进行排序
((SELECT COUNT(*) FROM `games` WHERE ((`home_score` > `away_score`) AND `home_team_id` = '.$team_id.')) +
(SELECT COUNT(*) FROM `games` WHERE ((`home_score` < `away_score`) AND `away_team_id` = '.$team_id.'))) AS `wins`
我想知道我是否可以使用某种形式的GROUP,或者mySQL可以知道$ team_id本身? 我也尝试过使用'team'表的多个JOIN,但它们也没有用。
谢谢,
丹
答案 0 :(得分:5)
让我们一步一步来做:
选择主场赢得的比赛和主场比分:
SELECT COUNT(*) as wins, SUM(G.home_score) as score FROM games G WHERE
G.team_id = T.team_id #See 3. query and you'll understand
G.home_score > away_score
我们将此结果称为HOME_GAMES。
选择获胜的比赛和客场比赛的得分:
SELECT COUNT(*) as wins, SUM(G.away_score) as score FROM games G
WHERE
G.team_id = T.team_id #See 3. query and you'll understand
G.away_score > G.home_score
我们将此结果称为AWAY_GAMES。
选择赢得的总比赛和总分:
SELECT (A.wins + H.wins) AS total_wins, (A.score + H.score) AS total_score FROM
(AWAY_GAMES) AS A, (HOME_GAMES) AS H, teams T
ORDER BY total_wins, total_score
==&GT;通过替换AWAY_GAMES和HOME_GAMES:
将所有内容放在一起SELECT (A.wins + H.wins) AS total_wins, (A.score + H.score) AS total_score FROM
(SELECT COUNT(*) as wins, SUM(G.away_score) as score FROM games G
WHERE
G.team_id = T.team_id #See 3. and you'll understand
G.away_score > G.home_score) AS A,
(SELECT COUNT(*) as wins, SUM(G.home_score) as score FROM games G
WHERE
G.team_id = T.team_id #See 3. and you'll understand
G.home_score > away_score) AS H,
teams T
ORDER BY total_wins, total_score
答案 1 :(得分:2)
也许这就是你要找的东西?
SELECT all_wins.team_id, SUM(all_wins.wins)
FROM (
SELECT
home_team_id as team_id,
SUM(IF(home_score > away_score,1,0)) as wins,
SUM(home_score - away_score) as points
FROM games
GROUP BY home_team_id
UNION ALL
SELECT
away_team_id as team_id,
SUM(IF(away_score > home_score,1,0)) as wins,
SUM(away_score - home_score) as points
FROM games
GROUP BY away_team_id
) all_wins
GROUP BY all_wins.team_id
ORDER BY SUM(all_wins.wins), SUM(all_wins.points)
ETA:原始答案不完整,我认为这应该更好。
内部的两个UNION一起查询将获得每个团队的主场和客场胜利。外部查询简单地总结了总赢数的主场和客场胜利。
答案 2 :(得分:0)
基于Eric的解决方案 - 如果其他人有类似的问题,这是我的最终查询 - 感谢大家的帮助。
SELECT `teams`.`id`, `teams`.`name`,
SUM(`all_wins`.`gp`) AS `gp`,
SUM(`all_wins`.`w`) AS `w`, SUM(`all_wins`.`l`) AS `l`, SUM(`all_wins`.`t`) AS `t`,
SUM(`all_wins`.`ptf`) AS `ptf`, SUM(`all_wins`.`pta`) AS `pta`
FROM (
SELECT
`home_team_id` as `team_id`,
COUNT(`home_score`) AS `gp`,
SUM(IF(`home_score` > `away_score`,1,0)) as `w`,
SUM(IF(`home_score` < `away_score`,1,0)) as `l`,
SUM(IF(`home_score` = `away_score`,1,0)) as `t`,
SUM(IFNULL(`home_score`,0)) as `ptf`,
SUM(IFNULL(`away_score`,0)) as `pta`
FROM `games`
GROUP BY `home_team_id`
UNION ALL
SELECT
`away_team_id` as `team_id`,
COUNT(`home_score`) AS `gp`,
SUM(IF(`away_score` > `home_score`,1,0)) as `w`,
SUM(IF(`away_score` < `home_score`,1,0)) as `l`,
SUM(IF(`away_score` = `home_score`,1,0)) as `t`,
SUM(IFNULL(`away_score`,0)) as `ptf`,
SUM(IFNULL(`home_score`,0)) as `pta`
FROM `games`
GROUP BY `away_team_id`
) `all_wins`
LEFT JOIN `teams` ON `all_wins`.`team_id` = `teams`.`id`
GROUP BY `all_wins`.`team_id`
ORDER BY SUM(`all_wins`.`w`) DESC, SUM(`all_wins`.`ptf`) DESC