|
发表于 2020-4-1 09:30:02
|
显示全部楼层
create table A(id int, title nvarchar(10), user1 int, user2 int)
insert A select 1, '文章1', 1, 2
create table B(id int, username nvarchar(10))
insert B select 1, '张三'
union all select 2, '李四'
select A.id, A.title, tmpA.username as user1, tmpB.username as user2 from A
left join B as tmpA on A.user1=tmpA.id
left join B as tmpB on A.user2=tmpB.id
--result
id title user1 user2
----------- ---------- ---------- ----------
1 文章1 张三 李四
(1 row(s) affected)
|
|