You *need* to have something that you can sort by. Most likely, that will be something like an Inserted_TS. Once you have that, the previous message would be the record with no parent that has the maximum inserted_ts less than the one you're on. You can do this via the type of query sonaliak did or using an analytical fundtion. But first, you need that field to sort on.
Yes i can sort out the table with the input as the sectionID. (i.e.) all the messages that belong to a particular section
select * from messages where sectionID=1 order by parentID desc;
This will give me all the messages for a particular section and would be ordered by parentID.
my output will be as follows:
messageID messge parentID sectionID title
1 first 0 1 first title
5 second 0 1 second title
7 first re 1 1 firstreply
9 second re 5 1 secondreply.
10 second re re 9 1 second second reply.
When i click the first reply to the second message (message second re and messageID 9) i need to get the 5 as my prev ID as this is the prevID for this message and 10 as my nextID as this is the next reply for this message. How can i do this one??