67,511
社区成员
发帖
与我相关
我的任务
分享
select a.* from feed_comment a left join feed b on a.feed_id=b.id left join feed_comment c a.id=c.to_id where user_id=5151 order by a.create_time desc
随手写个 希望表结构提供参考下--所以整个就可以简化
select * from feed_comment
where feed_id in (select id from feed where user_id =5151)
or to_id in (select id from feed_comment where user_id = 5151)
or user_id = 5151
--或者简化为
select a.* from feed_comment a, (select id from feed where user_id =5151) b, (select id from feed_comment where user_id = 5151) c
where a.feed_id = b.id
or a.to_id = c.id
or a.user_id = 5151
[/quote]问题就出在这条SQL语句,简化之后数据会对不上。to_id对应的是feed_comment里面的id.数据结构上面有--所以整个就可以简化
select * from feed_comment
where feed_id in (select id from feed where user_id =5151)
or to_id in (select id from feed_comment where user_id = 5151)
or user_id = 5151
--或者简化为
select a.* from feed_comment a, (select id from feed where user_id =5151) b, (select id from feed_comment where user_id = 5151) c
where a.feed_id = b.id
or a.to_id = c.id
or a.user_id = 5151
select a.* from feed_comment a, (select id from feed where user_id =5151) b
where a.feed_id = b.id
or a.to_id = b.id
union all
select a.* from feed_comment a, (select id from feed where user_id =5151) b, (select id, to_id from feed_comment ) c
where a.id = c.id
and c.to_id = b.id