有两个表,1个是文章表,一个是用户表
文章表中
id title user1 user2
1 文章1 1 2用户表
id username
1 张三
2 李四现在要得到记录为
1 文章1 张三 李四
请问如何构造SQL语句
文章表中
id title user1 user2
1 文章1 1 2用户表
id username
1 张三
2 李四现在要得到记录为
1 文章1 张三 李四
请问如何构造SQL语句
调试欢乐多
create table A(id int, title nvarchar(10), user1 int, user2 int)
insert A select 1, '文章1', 1, 2create 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)
wzb.id,wzb.title,yh1.username,yh2.username
From 文章表 wzb
Inner Join 用户表 yh1 on wzb.user1 = yh1.id
Inner Join 用户表 yh2 on wzb.user2 = yh2.id