MySQL INNER JOIN 别名
Posted
技术标签:
【中文标题】MySQL INNER JOIN 别名【英文标题】:MySQL INNER JOIN Alias 【发布时间】:2012-05-30 06:14:20 【问题描述】:有谁知道我如何在其中进行内部连接和别名值,这样它们就不会相互覆盖?如果您看到我的代码,它可能看起来更清楚:
SELECT home, away, g.network, g.date_start
FROM game g
INNER JOIN team t ON (
(t.importid = g.home) as home
OR
(t.importid = g.away) as away
)
ORDER BY date_start DESC
LIMIT 7
已解决(下面的帮助是我的最终查询)
SELECT
home.market AS home_market,
away.market AS away_market,
g.network,
g.date_start
FROM game AS g
INNER JOIN team AS home ON (
home.importid = g.home
)
INNER JOIN team AS away ON (
away.importid = g.away
)
ORDER BY g.date_start DESC
LIMIT 7
【问题讨论】:
【参考方案1】:您需要参加两次:
SELECT home.*, away.*, g.network, g.date_start
FROM game AS g
INNER JOIN team AS home
ON home.importid = g.home
INNER JOIN team AS away
ON away.importid = g.away
ORDER BY g.date_start DESC
LIMIT 7
【讨论】:
【参考方案2】:使用单独的列来表示连接条件
SELECT t.importid,
case
when t.importid = g.home
then 'home'
else 'away'
end as join_condition,
g.network,
g.date_start
FROM game g
INNER JOIN team t ON (t.importid = g.home OR t.importid = g.away)
ORDER BY date_start DESC
LIMIT 7
【讨论】:
嘿,这很酷——但我试图将“家”和“离开”放在同一行。我知道我的问题没有太大意义。 :) 如果我想要单独的主/行记录,这确实有效。以上是关于MySQL INNER JOIN 别名的主要内容,如果未能解决你的问题,请参考以下文章
Sql Server 与 MySql 在使用 update inner join 时的区别