MySQL INNER JOIN 别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10724324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:30:21  来源:igfitidea点击:

MySQL INNER JOIN Alias

mysqlselect

提问by JREAM

Does anyone know how I can do an inner joins and alias values within so they won't overwrite each other? It might look more clear if you see my code:

有谁知道我如何在其中进行内部连接和别名值,以便它们不会相互覆盖?如果你看到我的代码,它可能看起来更清楚:

    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

SOLVED(After the help below here is my final query)

已解决(在下面的帮助之后是我的最终查询)

    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

回答by Marcus Adams

You'll need to join twice:

您需要加入两次:

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

回答by juergen d

Use a seperate column to indicate the join condition

使用单独的列表示连接条件

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