我有一个表posts
,其中存储了用户的所有帖子,该表的结构如下
| post_id | post_user | post_content | post_date |
users表如下
| 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)
现在我希望用户分享帖子,为此我创建了一个表repost_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> |
答案 0 :(得分:0)
这就是你想要的吗?
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