类似的微博推断客户关系sql声明
2015-07-06 21:20
525 查看
类别似新浪微博的关注和共同关心
不知道别人是怎么设计的。
反正我是例如以下设计的
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'
不知道别人是怎么设计的。
反正我是例如以下设计的
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);
相关文章推荐
- 谈谈数据库的ACID
- mysql 数据库字段为 call 数据无法插入
- Mysql执行Update操作时会锁住表
- mysql数据库字段类型及其长度
- mongodb安装
- 在上一篇的基础上的追加——关于使用plsql developer的数据库表的导入和导
- MySQL主从复制
- SQL语句记录
- 基于mongodb, 设计游戏中的个人背包系统
- 解决jsqlparper不支持中文问题
- 【玩转Excel】Oracle PLSQL处理生成XLSX文件
- 从oracle中导出数据表 —— 使用命令行
- oracle实现分页-- 红黑
- 修改mysql 表的字符编码
- ubuntu 下mysql 设置允许远程连接
- Oracle pl/sql中的group by子句不能使用别名,应该这样写。
- Mysql 的源码包安装
- 一、Oracle分析函数入门
- 花擦节 Codis作者黄东旭细说分布式Redis架构设计和踩过的那些坑们
- MVC——数据库增删改查(Razor)