是的,你必须要正常化……
我为你创建了一些表格.
用户表
id(PK) name
新闻表
id (int 11)
Text
DateCreate (datetime)
user_id(FK with user table)
评论表
id (int 11)
Text
DateCreate (datetime)
news_id(int 11)(FK with news table)
user_id
评论表的子评论
id (int 11)
comments_id(FK with comments table)
Text
user_id(int)(FK with user table)
DateCreate (datetime)
现在为每条评论写下以下查询
对于循环,只需在注释表中使用一个循环.然后你得到每个评论的所有答案.
$query=query("select id from news");
while($q=mysql_fetch_assoc($query)){
//Here is each news
$query1=query("SELECT comments_id,comments FROM comments c WHERE c.news_id=$q['id']");
//Here is each comment
while($q1=mysql_fetch_assoc($query1)){
$query2=query("
SELECT sub.text,u.user_name FROM sub_comments AS sub
LEFT JOIN comments AS c
ON c.id=sub.comments_id
INNER JOIN user AS u
ON u.id=sub.user_id
WHERE c.id=$q1['comments_id']
");
while($q2=mysql_fetch_assoc($query2)){
//Here are sub comments for each comments
print $q2['text'];
}
}
}