【发布时间】:2014-12-12 06:17:03
【问题描述】:
我有一个用于存储 cmets 的表,其结构如下:
cmets
ID | post_id | comment | parent
1 | 1 | hello world | null
2 | 1 | reply 1 | 1
3 | 1 | reply 2 | 1
4 | 1 | reply 3 | 1
我想使用单个 sql 查询获取父评论及其所有回复。 目前我正在使用嵌套查询来检索特定父评论的回复,我知道这不是最佳实践。 我需要使用单个查询来完成此操作。
我当前的sql查询sn-p是:
parent_id =("select id,comment from comments where post_id='1' and parent='null'")["id"]
loop{
"select comment from comments where post_id='1' and parent= parent_id"
}
我正在使用这些嵌套查询来获取 cmets 及其回复,使用单个查询实现这一点的最佳做法是什么?
【问题讨论】:
-
所有条目的 postID = 1 那么你为什么要检查 parent_ID 或 parent
-
postID = 1 表示所有父评论和子 cmets 都属于 postID 1。我需要使用单个查询检索父评论及其所有回复。在父 paret 评论下显示回复
-
顺便说一句你是对的。我必须弄清楚!谢谢