MySQL INNER JOIN别名

时间:2012-05-23 16:42:06

标签: mysql select

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

    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

2 个答案:

答案 0 :(得分:39)

你需要加入两次:

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

答案 1 :(得分: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