嘗試......雖然我不知道你的表結構
Declare @Temp table
(
id int,
Name varchar(20)
)
Insert into @Temp
select 1, 'Bob'
union all
select 2, 'Mark'
union all
select 3, 'Shaun'
union all
select 4, 'Ryan'
union all
select 5, 'Steve'
union all
select 6, 'Bryan'
union all
select 7, 'Henry'
Declare @Temp2 table
(
iid int,
itmid int,
Name varchar(20)
)
Insert into @Temp2
select 1, 3, 'Thing'
union all
select 2, 2, 'This'
union all
select 3, 5, 'That'
union all
select 4, 1, 'They'
union all
select 5, 3, 'There'
union all
select 6, 5, 'Though'
union all
select 7, 6, 'Thought'
SELECT t1.[id], Row_Number() OVER (Order by t1.[id]) as RowNum
FROM @Temp t1
LEFT JOIN @Temp2 t2 ON t1.[id]=t2.[itmid]
ORDER BY t1.[id] ASC;
衛生署,你打我吧! –
非常感謝。我想知道SQL是值得的,呵呵;)我發誓我有一天會這樣! – ahmd0
很高興能幫到你:) – misha