需要SQL连接的帮助

时间:2011-07-01 22:01:13

标签: sql sql-server join

我有2个表格如下:


select 1 as id,
'A' as place 
into #places
UNION
select 2 as id,
'B' as place 
UNION
select 3 as id,
'C' as place
UNION
select 4 as id,
'D' as place

select 'x' as name,
2 as start,
3 as endd 
into #travel
UNION
select 'y' as name,
4 as start,
1 as endd
UNION
select 'z' as name,
1 as start,
3 as endd

select * from #places
select * from #travel

------------------------------

现在我想使用连接获取名称/起始位置/结束位置?

2 个答案:

答案 0 :(得分:1)

SELECT
 t.name,
 p1.place as start_place,
 p2.place as end_place
FROM
 travel t
 JOIN places p1 on t.start = p1.id
 JOIN places p2 on t.endd = p2.id

答案 1 :(得分:0)

select #travel.*, pstart.place as start, pend.place as end
from #travel inner join #places as pstart on pstart.id = #travel.start
inner join #places as pend on pend.id = #travel.end