我有一个表posts
,其中存储了用户的所有帖子,该表的结构如下
| post_id | post_user | post_content | post_date |
用户表如下
| user_id | username | user_joined |
用户关系表如下
| follower | following | rel_date |
这是我用来从用户关注的人那里获取帖子以显示他们的查询。
SELECT * FROM posts WHERE post_user in(SELECT follower from user_follow where following=$loggedin_user)
现在我希望用户共享帖子,为此我创建了一个表repot_user,如下所示
| repost_user | original_post_user | post_id | repost_date |
我想从用户关注的人那里获得帖子,其中也包括转发。。我该怎么做?
编辑:我的结果集应该是什么样子的
post_id | post_content | post_user | post_date | is_repost | repost_user | repost_date
例如,如果它的正常张贴行应该看起来像
23 | <some content> | 3 | <post date> | false | null | null |
如果它是一个转发,行将是
23 | <some content> | 3 | <post date> | true | 2 | <repost_date> |
这就是你想要的吗?
SELECT post_id,
post_content,
post_user,
post_date,
if(repost_user.post_id is null, 0,1) is_repost,
repost_user,
repost_date
FROM posts
LEFT join repost_user
on posts.post_id=repost_user.post_id
WHERE (repost_user in
(SELECT follower from user_follow where following=$loggedin_user)
or post_user in
(SELECT follower from user_follow where following=$loggedin_user) )
AND posts.post_id IS NOT NULL