2015-10-26 50 views
1

我的查詢看起來像自上加入表連接

Select m.cw_sport_match_id as MatchId, 
     m.season_id as SeasonId, 
     s.title as SeasonName, 
     c.title as ContestName 
from dbo.cw_sport_match m 
inner join dbo.cw_sport_season s 
    ON m.season_id = s.cw_sport_season_id 
inner join dbo.cw_sport_contest c 
    ON m.contest_id = c.cw_sport_contest_id 
Where s.date_start <= GETDATE() AND s.date_end >= GETDATE() 
order by s.date_start 

沒有我需要的sport_contest的母公司名稱(如果存在的話,它可以爲null)。所以基本上是一個自我連接,但不在查詢所在的同一張表上。所有我發現做自我連接的例子都不在另一個表上完成。 可以任何sql pro幫助我嗎? 那麼我怎樣才能加入與season_parent_id cw_sport_season本身並得到它的標題?

+0

如果你不加入同一張表,你爲什麼稱它爲自加入?你能澄清一下嗎? – dan08

+1

'自我加入不在另一張桌子上?如果他們在另一張桌子上,那麼它不是自聯接,而只是一個聯接。 –

+0

okey我很抱歉,我認爲自我加入是如果我再次加入對同一張表的引用。在這種情況下,它不是自我連接,我只是想加入到連接表本身的連接表上的引用... ^^ – LaurinSt

回答

2

如果我正確理解你的問題,你想outer joincw_sport_season表使用season_parent_id字段本身。也許這些線上的東西:

Select m.cw_sport_match_id as MatchId, 
     m.season_id as SeasonId, 
     s.title as SeasonName, 
     parent.title as ParentSeasonName, 
     c.title as ContestName 
from dbo.cw_sport_match m 
inner join dbo.cw_sport_season s 
    ON m.season_id = s.cw_sport_season_id 
inner join dbo.cw_sport_contest c 
    ON m.contest_id = c.cw_sport_contest_id 
left join dbo.cw_sport_season parent 
    ON s.season_parent_id = parent.cw_sport_season_id 
Where s.date_start <= GETDATE() AND s.date_end >= GETDATE() 
order by s.date_start 
+0

作品像一個魅力,謝謝! – LaurinSt