自加入加入 table
Self join on joined table
我的查询看起来像
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(如果有的话,它可以为空)。所以基本上是一个自连接,但不在与查询相同的 table 上。我找到的所有自连接示例都没有在另一个 table 上完成。
sql 专业人士可以帮助我吗?
那么我怎样才能将 cw_sport_season 本身与 season_parent_id 结合起来并获得它的标题呢?
如果我没有正确理解您的问题,您希望使用 season_parent_id
字段 outer join
cw_sport_season
table 自身。也许在这些方面:
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
我的查询看起来像
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(如果有的话,它可以为空)。所以基本上是一个自连接,但不在与查询相同的 table 上。我找到的所有自连接示例都没有在另一个 table 上完成。 sql 专业人士可以帮助我吗? 那么我怎样才能将 cw_sport_season 本身与 season_parent_id 结合起来并获得它的标题呢?
如果我没有正确理解您的问题,您希望使用 season_parent_id
字段 outer join
cw_sport_season
table 自身。也许在这些方面:
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