类似新浪微博的关注与相互关注
不知道别人是怎么设计的。
反正我是如下设计的
ID USER FRIEND
1 A B
2 B A
3 A C
ID为自增
user为发起关注者 friend为被关注者
现在需求如下。给出A、B两用户ID如何判断A与B的关系 很简单要,是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);
时间: 2024-08-15 07:59:39