一个表中多个字段同一个外键查询问题

wyman25 2009-08-06 02:16:21
学生表student(stu_id,stu_name),班级表class(monitor_id,vince_monitor_id) monitor_id为正班长学号,vince_monitor_id为副班长学号,而且monitor_id为student外键,vince_monitor_id也为student外键
用一条SQL查询语句,怎样查出一个班上正班长姓名和副班长姓名?
...全文
255 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2009-08-06
  • 打赏
  • 举报
回复

select
正班长姓名=b.stu_name,
副班长姓名=c.stu_name
from
class a,student b ,student c
where
a.monitor_id=b.stu_id
and
a.vince_monitor_id=c.stu_id
华夏小卒 2009-08-06
  • 打赏
  • 举报
回复



select [id]=monitor_id,stu_name from student s,class c where c.monitor_id=s.stu_id
union all
select [id]=vince_monitor_id,stu_name from student s,class c where c.vince_monitor_id=s.stu_id
feixianxxx 2009-08-06
  • 打赏
  • 举报
回复
select 
正班长姓名=b.stu_name,
副班长姓名=c.stu_name
from class a
join student b on a.monitor_id=b.stu_id
join student c on a.vince_monitor_id=c.stu_id
-狙击手- 2009-08-06
  • 打赏
  • 举报
回复
select a.stu_name ,b.stu_name
from class c
left join student a on a.stu_id = c.monitor_id
left join student b on b.stu_id = c.vince_nonitor_id
cngothic 2009-08-06
  • 打赏
  • 举报
回复
select * from student as s left jion class as c on s.stu_id = c.monitor_id or s.stu_id = vince_monitor_id
sdhdy 2009-08-06
  • 打赏
  • 举报
回复

select 正班长姓名=b.stu_name,副班长姓名=c.stu_name
from class a
left join student b on a.monitor_id=b.stu_id
left join student c on a.vince_monitor_id=c.stu_id

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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