Mysql查询与Union All和Join for League表
Posted
技术标签:
【中文标题】Mysql查询与Union All和Join for League表【英文标题】:Mysql query with Union All and Join for League table 【发布时间】:2012-09-25 10:40:27 【问题描述】:对于足球联赛,我有两张桌子:
“teams”是一个表格,将所有团队列为唯一条目('id', 'team_name') “matches”是显示匹配结果的表格:在列中 'home' 和 'away' 我保存每个游戏结果。 'home_team_id' 和 “away_team_id”是与球队表的关联。这是我的查询的样子:
SELECT Teams, Sum(P) as 'Matches', Sum(W) as 'win', Sum(D) as 'draw', Sum(L) as 'lost',
SUM(Pts) as 'points'
FROM (
SELECT home Teams, 1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE
WHEN home > away THEN 3
WHEN home = away THEN 1
ELSE 0
END PTS
FROM `matches`
UNION ALL
SELECT away Teams, 1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE
WHEN home < away THEN 3
WHEN home = away THEN 1
ELSE 0
END
FROM `matches`
) AS ERG
GROUP BY Teams
ORDER BY SUM(Pts) DESC
现在我想要团队表中的团队名称 (teams.team_name)。为了实现这一点,我尝试了几个没有运气的连接语句。
很明显,teams-table 可以包含没有参加比赛的球队。这些团队需要以零结果显示。
因此我尝试了 LEFT JOIN:
SELECT team_name AS 'Teams'
FROM `teams`
LEFT JOIN matches ON ( teams.id = matches.home_team_id )
就在最后的 ORDER 行之后。我收到一条错误消息。 我使用 mysql 5.1.44,所以嵌套选择应该不是问题。
有什么想法吗?
【问题讨论】:
1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'SELECT team_name AS 'Mannschaft' FROMteams
LEFT JOIN 匹配 ON 附近使用正确的语法(第 20 行的 team.id = '
当我尝试在sqlfiddle.com/#!2/7d97b/1 摆弄它时看不到错误(好的,这里没有数据,但我只是在检查错误)
【参考方案1】:
听起来你想做这样的事情。我更新了您的子查询以包含home_team_id
和away_team_id
,然后您将在teams
表上JOIN
以返回名称。:
SELECT Teams,
Sum(P) as 'Matches',
Sum(W) as 'win',
Sum(D) as 'draw',
Sum(L) as 'lost',
SUM(Pts) as 'points',
h.team_name as HomeTeam,
a.team_name as AwayTeam
FROM
(
SELECT home Teams,
1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE WHEN home > away THEN 3 WHEN home = away THEN 1 ELSE 0 END PTS,
home_team_id,
away_team_id
FROM `matches`
UNION ALL
SELECT away Teams,
1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE WHEN home < away THEN 3 WHEN home = away THEN 1 ELSE 0 END,
home_team_id,
away_team_id
FROM `matches`
) AS ERG
LEFT JOIN `teams` h
on ERG.home_team_id = h.home_team_id
LEFT JOIN `teams` a
on ERG.away_team_id = a.away_team_id
GROUP BY Teams, home_team_id, away_team_id
ORDER BY SUM(Pts) DESC
根据你的 cmets 编辑 #1,听起来你想要这个(见 SQL Fiddle with Demo):
SELECT TeamId,
t.team_name,
Teams,
Sum(P) as 'Matches',
Sum(W) as 'win',
Sum(D) as 'draw',
Sum(L) as 'lost',
SUM(Pts) as 'points'
FROM
(
SELECT home_team_id TeamId,
home Teams, 1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE
WHEN home > away THEN 3
WHEN home = away THEN 1
ELSE 0
END PTS
FROM `matches`
UNION ALL
SELECT away_team_id TeamId,
away Teams, 1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE
WHEN home < away THEN 3
WHEN home = away THEN 1
ELSE 0
END
FROM `matches`
) AS ERG
LEFT JOIN `teams` t
ON ERG.TeamId = t.id
GROUP BY Teams, TeamId
ORDER BY SUM(Pts) DESC
【讨论】:
现在我在“on Clause”中得到一个未知列“h.home_team_id” 非常感谢,但现在我得到:“'字段列表中的未知列'a.team_name'” @flyte321 没有看到数据和/或我根据您的原始问题猜测的完整表结构。teams
表中是否有一个名为 team_name
的字段?
是的,我在团队中有两列:“id”和“team_name”
表格很简单,看起来完全一样:id home_team_id home away_team_id away played created_at updated_at matchtime matchdate notes location
【参考方案2】:
你可以像这样使用外连接:
SELECT a.team_name, Sum(ERG.P) as 'Matches', Sum(ERG.W) as 'win', Sum(ERG.D) as 'draw', Sum(ERG.L) as 'lost',
SUM(Pts) as 'points'
FROM
Teams a
left outer join
(
SELECT home Teams, 1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE
WHEN home > away THEN 3
WHEN home = away THEN 1
ELSE 0
END PTS
FROM `matches`
UNION ALL
SELECT away Teams, 1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE
WHEN home < away THEN 3
WHEN home = away THEN 1
ELSE 0
END
FROM `matches`
) AS ERG
on a.team_name=ERG.Teams
GROUP BY a.team_name
ORDER BY SUM(Pts) DESC
这应该可以很好地为您解决问题。
我还写了一篇相当长的question and answer,它解释了联合、连接、外连接和各种其他好东西——我在回答这样的问题时会链接到这些东西。它非常长且详细,并详细解释了所有步骤。
【讨论】:
谢谢,但这会导致“‘字段列表’中出现“未知列‘b.P’” @flyte321 抱歉,在我编写查询的上半部分时错过了您的别名。编辑应该可以解决问题。 错误消失了,但现在出现了一个新错误:Unknown column 'a.teams' in 'on Clause' @flyte321 抱歉,我猜的是列名,看来您添加了语法(和列名)。在 select、join 和 group by 子句中将其更改为a.team_name
有望解决问题。
现在我不再收到错误消息了。但只有输出的第一行是正确的。看起来像这样team_name Matches win draw lost points 1. Superteam 2 0 0 2 0
所有其他行都用零填充,除了团队名称列是正确的以上是关于Mysql查询与Union All和Join for League表的主要内容,如果未能解决你的问题,请参考以下文章
mysql数据库多个表union all查询并排序的结果为啥错误
mysql数据库多个表union all查询并排序的结果为啥错误