我从数据库中提取行,这些行也会显示帖子的响应数。我能够抓住行,但是需要对连接表中的帖子进行统计,这需要post_id和comment_id - 我不确定如何处理这个或者它是否可行?
function build_forum_featured(){
global $dbc;
$q = "SELECT u.user_id, u.first_name, u.last_name, f.post_created, f.post_id, f.post_title, j.id
FROM users AS u
INNER JOIN user_post_join AS j ON u.user_id = j.user_id
INNER JOIN forum_post AS f ON f.post_id = j.post_id
WHERE featured_post = 2
ORDER BY post_created ASC
";
$r = mysqli_query ($dbc, $q); // Run the query.
// FETCH AND PRINT ALL THE RECORDS
while ($row = mysqli_fetch_array($r)) {
echo '
<div class="post">
<div class="col-group-2">
<a href="post_view.php?post_id='.$row["post_id"].'"><h3>'.$row["post_title"]. '</h3></a>
<p>By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["first_name"] . ' ' .$row["last_name"]. '</a> on: ' .date('d-m-Y', strtotime($row["post_created"])). '</p>
</div>
<div class="col-group-2">
<div class="post_count">
<h3 class="answer">0</h3> // count goes here!
<p class="answer">Responses</p>
</div>
</div>
</div>
';
}
基本上我在trykgi上做的是引用连接表并检查post_id以及与该帖子关联的注释数量。所以我有一个名为comment_post_join
的表,它接受post_id和comment_id,只添加行。
答案 0 :(得分:2)
在您的列列表中添加一个计数(*),以便在语句结尾处按user_id,post_id选择并添加一个组...