0

コメントテーブルがあり、人々がコメントを編集できるようにしています。コメントを上書きする代わりに、新しいコメントを作成し、それを「親」に関連付けます。さらに、子の情報を親に追加します。

id, user_id, comment_id__parent, comment_id__child, comment, created_dt

SQLフィドルはこちら

今私の問題は、特定のユーザーのすべてのコメントを取得したいが、コメントの最新の更新のみを取得したいということです。

これはしばらくの間私に大きな頭痛の種を与えています。

4

4 に答える 4

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;
于 2014-05-14T13:24:31.233 に答える
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
于 2014-05-14T13:24:33.907 に答える