我有一个评论表,允许人们编辑他们的评论。 我没有覆盖评论,而是创建了一条新评论,并将其与其父母"相关联。 另外,我将子信息添加到父级。
id, user_id, comment_id__parent, comment_id__child, comment, created_dt
现在我的问题是,我希望得到特定用户的所有评论,但只能获得评论的最新更新。
这让我头疼不已,我非常感谢你的投入!
答案 0 :(得分:2)
如果你的小提琴是正确的,你应该能够这样做:
SELECT * FROM comments
WHERE comment_id__child IS NULL AND user_id=1;
如果您在编辑时始终填充comment_id__child
'父级'注释,则此方法有效。
答案 1 :(得分:0)
表示uer_id = 1
select * from comments where user_id=1 order by created_dt desc;
答案 2 :(得分:0)
这完美无缺!你可以在sql中找到结果。
select * from comments group by user_id having count(id) = 1
UNION
select * from comments where user_id in (select user_id from comments group by user_id having count(id) > 1) and comment_id__child is null and comment_id__parent is not null;
答案 3 :(得分:0)
我想我找到了一个解决方案:
SELECT *
FROM comments
WHERE user_id = 1
AND ( (comment_id__parent IS NULL
AND comment_id__child IS NULL)
OR (comment_id__parent IS NOT NULL
AND comment_id__child IS NULL) )
ORDER BY created_dt DESC