评论和回复,加入3表查询

时间:2013-10-26 11:14:18

标签: php sql

我一直在寻找一段时间,但仍未找到最佳解决方案。我有三个表:帖子,成员资格和评论。我想要一个评论和回复系统。

这些是我的表格:

主要帖子:

post_id | image_url | image_thumb_url | title | text | date | sender_id 

会员资格:

user_id | username | pw | email | 

对帖子的评论(回复):

com_id | comment | post_id_fk | user_id_fk | ip | date

我目前有一个查询,用于检索包含会员资料数据的帖子:

SELECT * FROM `posts`
LEFT JOIN `membership` ON `posts`.`sender_id` = `membership`.`user_id`
UNION SELECT * FROM `posts`
      RIGHT JOIN `membership` ON `posts`.`sender_id` = `membership`.`user_id`
      ORDER BY `post_id` DESC LIMIT $limit $start;

我现在想要添加对每个帖子的回复,其中包含会员资料数据。有没有人有建议或者更好的解决方案?

1 个答案:

答案 0 :(得分:0)

SELECT * FROM posts AS P
LEFT JOIN comments AS C
ON P.post_id = C.post_id_fk
LEFT JOIN membership AS POST_MEMBER
ON POST_MEMBER.user_id = P.sender_id
LEFT JOIN membership AS REPLY_MEMBER
ON REPLY_MEMBER.user_id = C.user_id_fk