三张表:
student(sid, sname)
book(bid, bname)
borrow(sid, bid)
其中borrow表中与student 和 book之间存在外键关系现在要 查询借阅所有图书的同学
写了一条sql语句select sid, sname
from student
where not exists
(
select *
from book
where not exists
(
select *
from borrow
where student.sid=sid and book.bid=bid
)
)
经测试时对的, 但是我不太明白, 求哪位解释一下这条sql, 谢谢!
student(sid, sname)
book(bid, bname)
borrow(sid, bid)
其中borrow表中与student 和 book之间存在外键关系现在要 查询借阅所有图书的同学
写了一条sql语句select sid, sname
from student
where not exists
(
select *
from book
where not exists
(
select *
from borrow
where student.sid=sid and book.bid=bid
)
)
经测试时对的, 但是我不太明白, 求哪位解释一下这条sql, 谢谢!
解决方案 »
- javascript相对路径,绝对路径问题
- 找错误 为什么点查看文件列不出来
- 大家是如何管理项目中的常量、配置等参数的?
- jsp页面传递%问题!
- 转让sun公司 314 考试券【上海】
- Struts1.2和1.3有什么区别吗?
- 加急!!!javascript:checkHyperlink问题
- Tomcat服务已经启动了,但是浏览器却无法访问Tomcat的默认主页http://localhost:8080
- SSH报错
- 奇怪了,昨天发的帖子找不到了,再问如何在weblogic6上发布javabean? 解决问题100分马上给!
- 关于连接池proxool与hibernate结合
- struts2+spring+ibatis 如何在web容器启动的时候,就得到spring注入的bean?
完全可以简单点实现的:
select s.sid,s.sname
from student s, book b, borrow bw
where s.sid=bw.sid and bw.bid=b.bid
LZ的是借阅所有书的同学
1楼的是所有借过书的同学
from student
where not exists
(
select *
from book
where not exists
(
select *
from borrow
where sid=aaID and book.bid=bid
)
)有内往外看: select *
from borrow
where sid=aaID and book.bid=bid这个语句查询aaID这人借的全部书。
再看先反向检验式理解,假设ID等于aaID同学是借阅了全部的书:则语句变为
[code=SQL]
select *
from book
where not exists
(
aaID借的全部书
)查询出aaID没有借的书,如aaID全借了,则查出的记录数0.
再看:select sid, sname
from student
where not exists
(
aaID没借的书。
)若aaID没借的书不存在,则返回真。就把aaID的名字和ID查出来。
楼主明白了不???
借楼上:否定之否定。
第一步:
select *
from book
where not exists
(
select *
from borrow
where book.bid=bid
)
这句话主要意思为找出没有借阅过的图书第二步:
select *
from book
where not exists
(
select *
from borrow
where @sid=book.sid and book.bid=bid
)
找句话的意思为,出现未借阅图书时,找出那条记录的sid(@sid)第三步:
select sid, sname
from student
where not exists
(
select *
from book
where not exists
(
select *
from borrow
where student.sid=sid and book.bid=bid
)
)找出不在@sid这个集合内的记录。即借阅了所有图书的sid