2008-06-23
表的自连接同子查询有区别?
运用自连接同子查询之间有什么区别?请大家给点意见
SELECT R1.readerid,R1.readername,R1.unit,R1.bookcount
FROM ReaderInfo AS R1,ReaderInfo AS R2
WHERE R2.readerid=9704
AND R1.bookcount>R2.bookcount --连接关系
ORDER BY R1.bookcount
SELECT readerid,readername,unit,bookcount
FROM ReaderInfo
WHERE bookcount>(SELECT bookcount --使用子查询
FROM ReaderInfo
WHERE readerid=9704)
ORDER BY bookcount
- by beyondsanli
- 浏览 (167)
- 评论 (3)
- 相关推荐


评论
SELECT bookcount --使用子查询
FROM ReaderInfo
WHERE readerid=9704
方式一没有这个限制
不过,当这个子查询的结果有多条结果的时候
方式一得到的是个迪卡尔积
这种情况下,为了避免因为迪卡尔积而得到重复纪录,可以用exists方式
SELECT R1.readerid, R1.readername, R1.unit, R1.bookcount FROM ReaderInfo A WHERE exists ( select 1 from ReaderInfo R2 where R2.readerid=9704 and R2.bookcount < R1.bookcount ) ORDER BY R1.bookcount