这个查询怎么做?

krack2002 2003-08-19 02:55:21
table1其中的两个域为: userid、username
table2其中的两个域为: userid1、userid2(这两个域和table1中userid的关系都是多对1)

我想做一次查询后,得到userid1和userid2分别对应的username,这个查询怎么做?

...全文
16 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrazyFor 2003-08-19
  • 打赏
  • 举报
回复
select a1.username,a2.username
from table2 b left join table1 a1 on b.userid1=a1.userid
left join table1 a2 on b.userid2=a2.userid

dafu71 2003-08-19
  • 打赏
  • 举报
回复
SELECT username
FROM table1,table2
WHERE table1.userid=table2.userid1 or table1.userid=table2.userid2
txlicenhe 2003-08-19
  • 打赏
  • 举报
回复
select a.username,b.username
From table2 c
join table1 a on c.userid1 = a.userid
join table1 b on c.userid2 = b.userid
hjb111 2003-08-19
  • 打赏
  • 举报
回复
create view v_table2
as
select a.* from table2 a,table1 b where a.userid=b.userid and b.username=条件
Go
krack2002 2003-08-19
  • 打赏
  • 举报
回复
to xelite(潜龙):
这样分别查我会。我想问得是一次可否查询得到?怎样做这个视图?
zarge 2003-08-19
  • 打赏
  • 举报
回复
select t1a.username, t1b.username
from table2 t2 inner join table1 t1a on t2.userid1 = t1a.userid
inner join table1 t1b on t2.userid2 = t1b.userid
hjb111 2003-08-19
  • 打赏
  • 举报
回复
select a.* from table2 a,table1 b where a.userid=b.userid and b.username=条件
xelite 2003-08-19
  • 打赏
  • 举报
回复
--userid1对应的username
SELECT username
FROM table1,table2
WHERE table1.userid=table1.userid1

--userid2对应的类似

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧