三表联查sql(三表联查的SQL语句)

本文目录
三表联查的SQL语句
这问题交给我吧,假设学生表叫student,课程表叫class,选课表叫choose
1.三层嵌套的问题
select student.name from student where student.id IN
(select choose.sid from choose where choose.cid NOT IN
(select class.id from class where class.teacher=’李明’))
2.一个内连接,一个嵌套
select student.name,avg(choose.score) from
student inner join choose on student.id=choose.sid
where student.id IN
(select choose.sid from choose
where choose.score《’60’
group by choose.sid
having count(choose.sid)》=2)
gruop by student.id
3.一个联合查询,一个嵌套查询
select student.name from student
where student.id IN
(select c1.sid from choose c1 where choose.cid=’1’
union
select c2.sid from choose c2 where choose.cid=’2’
on c1.sid=c2.sid
)
4.好吧,看起来很难,其实就是自连接查询和行列交换的问题
select student.id,
(case choose.id when ’1’ then choose.score end) as 1号课成绩,
(case choose.id when ’2’ then choose.score end) as 2号课成绩,
from student inner join choose on student.id=choose.sid sc1,
student inner join choose on student.id=choose.sid sc2
where sc1.id=’1’
and sc2.id=’2’
and sc1.score》sc2.score
5.至于你说的insert报错的问题,我想可能是因为学生ID和课程ID这两个外键有重复的值,
你可以检查下,实在不行删除外键,插入数据,在这里外键对你最后要的结果影响不大。
纯手工打造~有帮助记得给分哈
sql三表联查,有三个表,通过三个表中共同的值查询其他结果,类似于 where a.id=b.id=c.id 请问怎么写
--先要确认表也表之间的关系 就是外键关系
如果A表中有B表与C表字段.
--这样关系确认.就可以查询到符合这种关系的数据
select * from A表 a,B表 b,C表 c where a.bid=b.bid and a.cid=c.cid
如有问题可以追问,我当及时回答.
希望能帮到你!

更多文章:
unlikely(not likely和unlikely含义一样吗)
2025年10月23日 20:45
kindeditor 在dialog中居中(在Iframe中使用jquery ui dialog 怎么使弹出窗口居于浏览器中间)
2025年7月27日 23:15
ASP.NET MVC 4框架揭秘:Controller类型的缓存?RxCache缓存框架
2026年6月1日 06:00
java编程工具免费(请问java编写程序除了用eclipse,还可以用什么我主要是想做web前端的,有没有什么视图化的软件啊)
2025年8月4日 12:30
memcached性能(PHP中4个加速,缓存扩展的区别和选用建议)
2025年9月8日 08:45
2 3表示什么python(python语句:print(*[1,2,3]),是什么意思)
2026年8月14日 22:30
java下载了打不开(为什么下载了java也玩不了我的世界)
2026年5月17日 05:15
timeformat函数(GetTimeFormat怎么用,各个参数尤其是最后一个有点看不懂)
2025年11月13日 04:30
sqlite3规则(sqlite3数据库 运行一段时间后 命令行操作出现:Error:unsupported file format)
2025年10月25日 22:45
extensional mentality的意思(mentality是什么意思)
2026年1月6日 13:00














