0

我有一个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)

现在我希望用户分享帖子,为此我创建了一个表 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> |

4

1 回答 1

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
于 2013-07-30T11:12:11.567 回答