I have two tables
post Table
post_id
post_name
post_summary
date
post_reply Table
parent_id
post_id
So what I am doing is I inset a new post detail in post table and if there is any reply for that post then I put all the detail in post table itself and also stores the main post id as parent_id and replyed post id as post_id in post_reply.
Now I want to fetch all the posts and their reply under the main post if there is any and want to display the same on my jsp page.
I can’t write a single query to fetch these details. I can do it by first fetching all the post whose ids are not in post_reply and then loop on all post and then fetch the main post and replied post one by one.
I just want to fetch all these in one query. Is this possible? I am using mysql as database and using spring mvc with mybatis. So I am fecthing the whole list and putting it in my model attribute and display it on jsp page.
If someone can suggested me a better way of achieving this like changing my database structure or any other solution then that would be great.
table post
table post_reply
Use this query it is working:
It will return the reply with parent post and parent post only if there is no reply..