热线电话:13121318867

登录
2019-03-16 阅读量: 585
SQL查询

问题描述:

select t.*
from talk t
where t.user_id = 2
Union
select t.*
from talk t, user_contact c
where c.user_id = 2 and c.contact_type = 1 and c.be_user_id = t.user_id;

内连接或者左连接

存在一种情况就是这个user在user_contact 表里面没数据,代表他没关注任何人

user_id = 1 是用户的id,也就是我的id

select t.*
from talk t left join user_contact c on c.user_id = t.user_id
where t.user_id = 1 or (c.user_id = 1 and c.be_user_id = t.user_id and c.contact_type = 1);

这样写查询出来有重复数据

user_contact是用户关系表

talk 是帖子表

帖子表talk有存发布帖子的用户的user_id
然后用户关系表user_contact 存了 user_id,be_user_id是被关注的用户的id,contact_typ_type是用户关系类型,1为关注类型

解决方法:

select * from talk 
where user_id=1
or user_id in (
select be_user_id from user_contact where user_id=1 and contact_type = 1
);

27.9157
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子