MYSQL牛逼应用语句

accountbase :用户信息表
accountattention:用户关注或粉丝的关联关系表
查询粉丝列表,同时显示当前用户与粉丝用户是否是相关关注状态,返回 1:代表互关,0:只是粉丝
100000009 :代表某个用户id

查询代码如下:

	select accid, accountname,wxnickname,wxuserpic,accountpic,1 from accountbase bb where exists (
			select a.accid from  (select accid, attentaccid from accountattention  where attentaccid = 100000009 and delflag = 0 ) a  where 
				(select count(*) from (select accid, attentaccid from accountattention where accid = 100000009 and  delflag = 0 ) b where a.accid = b.attentaccid )=1 
			and a.accid = bb.accid 
	)		
	union 	
	select accid, accountname,wxnickname,wxuserpic,accountpic,0 from accountbase bb where exists (
			select a.accid from  (select accid, attentaccid from accountattention  where attentaccid = 100000009 and delflag = 0 ) a  where 
				(select count(*) from (select accid, attentaccid from accountattention where accid = 100000009 and  delflag = 0 ) b where a.accid = b.attentaccid )=0 
			and a.accid = bb.accid 		
	)		

图:
在这里插入图片描述

解释语句:
获取 100000009 的粉丝用户id:
select accid, attentaccid from accountattention where attentaccid = 100000009 and delflag = 0
获取100000009 的关注用户id:
select accid, attentaccid from accountattention where accid = 100000009 and delflag = 0
获取是100000009的粉丝但不是100000009的关注用户id:
select a.accid from (select accid, attentaccid from accountattention where attentaccid = 100000009 and delflag = 0 ) a
where (
select count(*) from (select accid, attentaccid from accountattention where accid = 100000009 and delflag = 0 ) b where a.accid = b.attentaccid
) =0
例如:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值