我有評論回覆(只有一個級別)的功能。所有評論可以有多達答覆,但沒有答覆可以有他們的進一步答覆。如何在MYSQL中進行評論回覆查詢?
所以我的數據庫表結構是像下面
Id ParentId Comment
1 0 this is come sample comment text
2 0 this is come sample comment text
3 0 this is come sample comment text
4 1 this is come sample comment text
5 0 this is come sample comment text
6 3 this is come sample comment text
7 1 this is come sample comment text
在上述結構中,commentid,1(有2個回覆)和3(1回覆)具有回覆。因此,要獲取評論和他們的回覆,一個簡單的方法是首先獲取ParentId爲0的所有註釋,然後通過運行while循環獲取該特定commentId的所有回覆。但是,如果我在特定記錄上有大約200條評論,那麼這似乎正在運行數百個查詢。
所以我想做一個查詢,它將按順序獲取評論與他們的答覆,如下所示;
Id ParentId Comment
1 0 this is come sample comment text
4 1 this is come sample comment text
7 1 this is come sample comment text
2 0 this is come sample comment text
3 0 this is come sample comment text
6 3 this is come sample comment text
5 0 this is come sample comment text
我也有我的評語表評論日期列,如果有人想與評論的查詢使用。
所以最後我想通過使用一個單一的mysql查詢獲取所有的評論和他們的答覆。請告訴我我該怎麼做?
謝謝
不錯,我想知道如何做到這一點。:) – 2009-06-02 00:37:09