sql查询有关问题(使用类似关系代数中的除法)

sql查询问题(使用类似关系代数中的除法)
本帖最后由 u010380055 于 2015-01-17 23:25:16 编辑
有如下4个表:users(uid),
follower(follower,followee),
rechirp(orginal,rechirp),
chirp(cid,author).
一个简单的微博管理系统,chirp就是发的微博,有id号和作者。rechirp就是转发表,字段分别是原始的(被转发的)微博,和转发的微博。follower就是关注表,follower是关注人,followee是被关注人。follower和followee和author都是uid的外键,orginal 和rechirp都是cid的外键。
问题是:找到至少转发了所有关注他的人的一条微博的人。比如,a 被bcd关注,如果a转发了bcd中每个人的至少一条微博,a就是查询结果。如果a没有转发bcd中任何一个人的任何一条微博,则排除。
我知道是用除法,但具体怎么用sql做我试了很久都不行,求指导!
------解决思路----------------------
select * from users where exists (select 1 from follower where followee=uid) and not exists
(
select 1 from follower where followee=uid and not exists
(
select 1 from rechirp, chirp a, chirp o where rechirp=a.cid and orginal=o.cid and a.author=followee and o.author=follower
)
)