自联接加入表

时间:2015-10-26 19:55:49

标签: sql sql-server join self-join

我的查询看起来像

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的名称parent(如果有的话,它可以为null)。所以基本上是一个自连接,但在查询的同一个表上没有。我找到的所有自我连接的例子都没有在另一个表上完成。 任何sql pro都可以帮帮我吗? 那么如何将cw_sport_season本身与season_parent_id一起加入并获得它的标题呢?

1 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,您希望使用 def escapeSQLDelimiters(dataToEscape: String) = { dataToEscape flatMap { c => c match { case '\\' => "\\\\" case '\u0000' => "\\0" case '\n' => "\\n" case '\t' => "\\t" case '\r' => "\\r" case '\b' => "\\b" case '\'' => "\\\'" case '\"' => "\\\"" case _ => c.toString } } } 字段outer join cw_sport_season表格。也许就是这些问题:

season_parent_id