The question itself might be written wrong so if anyone have better idea how to define it, please, edit the question.
Structure
Only important columns.
comments
- comment_id
- comment_parent_id
- user_id
- comment_text
posts
- post_id
- user_id
- post_text
comments_to_post (comments_to_foos, comments_to_bars, ...)
- comment_id
- post_id (foo_id, bar_id, ...)
comments_to_post
has only those connections to comments where comment_parent_id
on comments
is NULL
since every other comment is just a child of another comment.
I'm struggling with query which in results will give me comments assigned to specific post (by post_id
) with its children.
I tried to select from comments
and join comments_to_post
where post_id = ?
but no wonder I got only comments with NULL
comment_parent_id
since only them are connected via comments_to_post
.
How to add to the result also those comments which are children?
Example data:
comments
comment_id comment_parent_id
1 NULL
2 1
3 1
4 NULL
5 4
posts
post_id
10
20
comments_to_posts
comment_id post_id
1 10
4 20
Expected result
... where post_id = 10
comment_id
1
2
3