类别似新浪微博的关注和共同关心
不知道别人是怎么设计的。
反正我是例如以下设计的
ID USER FRIEND
1 A B
2 B A
3 A C
ID为自增
user为发起关注者 friend为被关注者
如今需求例如以下。
给出A、B两用户ID怎样推断A与B的关系 非常easy要。是A关注B。还是A与B相互关注 一条SQL尽可能的利用索引,不用OR,尽可能的高速 (不要求得到B是否关注A)
select * from xxxxx where USER='A' and FRIEND='B' union all select * from xxxxx where USER='B' and FRIEND='A' create table tb_user_concern (ID int auto_increment primary key, USER varchar(32), FRIEND varchar(32) ); -- 查询和A相互关注的USER select * from tb_user_concern a where user = 'A' and exists (select * from tb_user_concern b where a.user = b.friend and a.friend = b.user); -- 查询A关注,但未关注A的USER select * from tb_user_concern a where user = 'A' and not exists (select * from tb_user_concern b where a.user = b.friend and a.friend = b.user); -- 查询关注A,但A未关注的USER select * from tb_user_concern a where friend = 'A' and not exists (select * from tb_user_concern b where a.user = b.friend and a.friend = b.user);版权声明:本文博客原创文章。博客,未经同意,不得转载。
转载于:https://www.cnblogs.com/bhlsheji/p/4625441.html
相关资源:数据结构—成绩单生成器