我想用一些過濾器製作一個簡單的搜索系統,其中一個過濾器是通過'流行'來排序,這將基本上將首先評論最多的帖子和評論最少的帖子持續。 我有2個表格,其中一個用於包含所有帖子(標題,內容,圖片,postId)的帖子,另一個用於評論,其中包含所有評論(內容和postId(用於驗證評論是從哪個帖子發出的)我的查詢樣子:按照評論數量訂購帖子
$search = $db->prepare("SELECT comments.postId, comments.commentId, COUNT(comments.commentId) AS comms, posts.title, posts.postDate, posts.postId, posts.content,
FROM comments, posts, users
WHERE comments.postId = posts.postId AND posts.content LIKE ? OR posts.title LIKE ?
GROUP BY comments.commentId
ORDER BY comms DESC
LIMIT 100");
$search->execute(array("%$q%", "%$q%"));
來顯示搜索結果我用:
foreach($search as $s) {
?>
<a href="stream.php?viewPost=<?php echo $s['postId'];?>">
<h5>
<?php echo $s['title']; ?>
</h5>
</a>
<small>Posted on <?php echo $s['postDate']; ?> </small>
<div class="divider"></div>
<?php
}
?>
但是,當我尋找的東西,而不是顯示評論最多的第一個帖子,這表明1周後(實際上有大多數評論),它會一遍又一遍地顯示帖子。 如果我刪除
GROUP BY comments.commentId
從查詢它只會顯示該職位一次。
如果有人能告訴我我在做什麼錯在這裏,因爲我真的沒有看到它。
它不顯示任何東西,如果我使用這個查詢? – SirMaxime
@SirMaxime。 。 。那麼我會假定'where'子句沒有匹配。 –