I’m developing a information system, and I’ve and message center. And I want to display the messages like Outlook or Gmail “conversation”.
In my message table I have the following columns:
- MessageId (PK)
- Date
- From (FK)
- To (FK)
- Body
- Subject
- State
- Subject
- ReplyId (FK), null
First I get the messages that don’t have replyId, to show the “latest” message.
But I don’t get the conversation, when any one click on message… 🙁
Any one could help me with the query?
Thanks in advance
EDIT:
I add an extra field to the table, the Id of last reply to the conversation, so I can get all conversation message at one time!
🙂
You want to use a
CTEto accomplish this, which is essentially a parent-child query:If you’re using SQL Server 2008, you may want to look into using a
hierarchyidcolumn on your table to avoid the CTE all together.