我有一个帖子表和一个评论表,对于单个帖子,其中会添加一些评论。有一个 php 表,我在其中显示数据。
在此表中,我想在添加帖子时显示帖子的最新评论。
帖子表,添加帖子的地方:
Posts
+-------+---------+
| id | Post |
+-------+---------+
| 1 | ABC |
| 2 | DEF |
| 3 | GHI |
+-------+---------+
评论表,其中将向特定帖子添加评论数量:
Comments
+----------+---------+
| Post_id | Comment |
+----------+---------+
| 1 | A |
| 1 | D |
| 3 | F |
| 3 | V |
| 3 | B |
+----------+---------+
这里的post_id取自post表的id。
我希望带有最新评论的帖子显示在此表中:
<table>
<thead>
<tr>
<th>Post</th>
</tr>
</thead>
<tbody>
<?php foreach($results as $key=>$row) { ?>
<tr>
<td><?php echo $row['Comment'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
例如:
+----------+---------+
| Post | Comment |
+----------+---------+
| ABC | D |
| GHI | B |
+----------+---------+
提前致谢。
最佳答案
这对你有用!
SELECT comment FROM comments WHERE comment_id = (SELECT comment_id
FROM comments WHERE post_id = 1 ORDER BY comment_id DESC LIMIT 1);`
表结构
表格数据
查询结果
关于php - 显示最近输入的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35574641/