33

互いに上書きしないように、内部結合とエイリアス値を内部で実行する方法を知っている人はいますか? 私のコードを見ると、より明確に見えるかもしれません:

    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
4

2 に答える 2

53

2 回参加する必要があります。

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
于 2012-05-23T17:20:35.400 に答える
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
于 2012-05-23T16:45:58.563 に答える