select a.topID,b.nick,c.nick from top as a left join basic as b on a.id1 = b.id left join basic as c on a.id2 = b.id
抱歉,更正一下: select a.topID,b.nick,c.nick from top as a left join basic as b on a.id1 = b.id left join basic as c on a.id2 = c.id
select a.*,b.* from top a inner join basic b on (a.id1=b.id or a.id2=b.id)
declare @basic table(id int,nick char(10)) insert @basic select 1, 'haha' union all select 2, 'hoho' union all select 3, 'hehe'
declare @top table(topid int,id1 char, id2 char) insert @top select 1, 1 ,2 union all select 2, 1 , 3 select * from @top select topid,b.nick,c.nick from @top , @basic as b ,@basic as c where id1=b.id and id2=c.id ----------------------结果 1 haha hoho 2 haha hehe ------------------------------
from top as a
left join basic as b on a.id1 = b.id
left join basic as c on a.id2 = b.id
select a.topID,b.nick,c.nick
from top as a
left join basic as b on a.id1 = b.id
left join basic as c on a.id2 = c.id
insert @basic
select 1, 'haha' union all
select 2, 'hoho' union all
select 3, 'hehe'
declare @top table(topid int,id1 char, id2 char)
insert @top
select 1, 1 ,2 union all
select 2, 1 , 3
select * from @top
select topid,b.nick,c.nick from @top , @basic as b ,@basic as c where id1=b.id and id2=c.id
----------------------结果
1 haha hoho
2 haha hehe
------------------------------