MySQL查询分层表结构

时间:2015-10-29 08:43:36

标签: php mysql loops hierarchical-data commenting

我正在尝试在我的文章下面建立一个评论系统,用户也可以回复评论。只有登录用户才能发布评论或其回复。

我在MySQL中创建了4个表。

用户表



<style type="text/css">
	table.tableizer-table {
	border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
} 
.tableizer-table td {
	padding: 4px;
	margin: 3px;
	border: 1px solid #ccc;
}
.tableizer-table th {
	background-color: #104E8B; 
	color: #FFF;
	font-weight: bold;
}
</style>
&#13;
<table class="tableizer-table">
<tr class="tableizer-firstrow"><th>id</th><th>email</th></tr>
 <tr><td>1</td><td>abc@gmail.com</td></tr>
 <tr><td>2</td><td>xyz@gmail.com</td></tr>
 <tr><td>3</td><td>pqr@gmail.com</td></tr>
 <tr><td>4</td><td>abc@yahoo.com</td></tr>
 <tr><td>5</td><td>xyx@yahoo.com</td></tr>
</table>
&#13;
&#13;
&#13;

个人资料表

&#13;
&#13;
<style type="text/css">
	table.tableizer-table {
	border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
} 
.tableizer-table td {
	padding: 4px;
	margin: 3px;
	border: 1px solid #ccc;
}
.tableizer-table th {
	background-color: #104E8B; 
	color: #FFF;
	font-weight: bold;
}
</style>
&#13;
<table class="tableizer-table">
<tr class="tableizer-firstrow"><th>id</th><th>user_id</th><th>fname</th><th>lname</th></tr>
 <tr><td>1</td><td>3</td><td>PQR</td><td>Gmail</td></tr>
 <tr><td>2</td><td>2</td><td>XYZ</td><td>Gmail</td></tr>
 <tr><td>3</td><td>1</td><td>ABC</td><td>Gmail</td></tr>
 <tr><td>4</td><td>5</td><td>XYZ</td><td>Yahoo</td></tr>
 <tr><td>5</td><td>4</td><td>ABC</td><td>Yahoo</td></tr>
</table>
&#13;
&#13;
&#13;

文章表

&#13;
&#13;
<style type="text/css">
	table.tableizer-table {
	border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
} 
.tableizer-table td {
	padding: 4px;
	margin: 3px;
	border: 1px solid #ccc;
}
.tableizer-table th {
	background-color: #104E8B; 
	color: #FFF;
	font-weight: bold;
}
</style>
&#13;
<table class="tableizer-table">
<tr class="tableizer-firstrow"><th>id</th><th>article_title</th><th>article_content</th></tr>
 <tr><td>1</td><td>Helooooooo</td><td>Hi, How are you?</td></tr>
 <tr><td>2</td><td>Hiiiiiiiiiiii</td><td>Hey, How are you?</td></tr>
 <tr><td>3</td><td>Heeeeeey</td><td>Hello, How are you?</td></tr>
</table>
&#13;
&#13;
&#13;

评论表

&#13;
&#13;
<style type="text/css">
	table.tableizer-table {
	border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
} 
.tableizer-table td {
	padding: 4px;
	margin: 3px;
	border: 1px solid #ccc;
}
.tableizer-table th {
	background-color: #104E8B; 
	color: #FFF;
	font-weight: bold;
}
</style>
&#13;
<table class="tableizer-table">
<tr class="tableizer-firstrow"><th>id</th><th>article_id</th><th>user_id</th><th>parent_comment_id</th><th>Comment</th></tr>
 <tr><td>1</td><td>2</td><td>5</td><td>0</td><td>Great Article</td></tr>
 <tr><td>2</td><td>2</td><td>2</td><td>0</td><td>Nice Article</td></tr>
 <tr><td>3</td><td>2</td><td>4</td><td>1</td><td>I agree Great Article</td></tr>
 <tr><td>4</td><td>2</td><td>1</td><td>1</td><td>I also agree Great Article</td></tr>
 <tr><td>5</td><td>2</td><td>3</td><td>0</td><td>Bad Article</td></tr>
 <tr><td>6</td><td>2</td><td>4</td><td>2</td><td>OK Artilce</td></tr>
</table>
&#13;
&#13;
&#13;

我希望我的输出显示为:

Name: XYZ Yahoo (Comment)
Great Article   

Name: ABC Yahoo (Reply on 1st Comment)
I agree Great Article

Name: ABC Gmail (Reply on 1st Comment)
I also agree Great Article

Name: XYZ Gmail (Comment)
Nice Article    

Name: ABC Yahoo (Reply on 2nd Comment)
OK Artilce

Name: PQR Gmail (Comment)
Bad Article 

在上面的评论系统中,我使用分层表结构来存储评论及其回复。

希望我能够解释我的问题。

有人可以帮我解决MySQL语句和php代码

1 个答案:

答案 0 :(得分:0)

我会稍微调整order by子句,这样你就可以在php端获得正确顺序的所有帖子,所以你可以简单地遍历结果集。

$sql = 'SELECT  c.id, parent_comment_id,`comment`, p.fname, p.lname FROM comment c INNER JOIN profile p on c.user_id=p.user_id INNER JOIN WHERE article_id='.$article_id . ' ORDER BY IF(parent_comment_id=0,id,parent_comment_id) ASC, id ASC;'

在循环浏览结果集时,我会构建一个数组来保存每条记录的ID,以确定它们在评论中的排名。虽然,我应该注意,在论坛上你很少看到评论是对第一条评论的回应的引用,你通常可以看到评论是对sy在这样的时间发表的评论的回应。无论如何,辅助数组也可以保存记录的用户名和时间戳。

//outside the loop
//initialise rowcount variable to 0
$rowcount=0
...

//within the loop
//rowcount variable
$rowcount++;
$helper[$row['id']]=$rowcount;
...
//if a comment has a parent comment, get the parent's rownumber
echo 'Reply to ' . $helper[$row['parent_comment_id']] . '. comment';