我在使用此查询时遇到问题,该查询根据不同表中的回复数对论坛主题进行排序。我在 where 之前尝试了 Left join ,但在我的 while 循环中遗漏了一些数据。
SELECT forum_topics.*, COUNT(forum_posts.comment_id) AS replies
FROM forum_topics
WHERE forum_topics.subcat_id = '$subcatid'
LEFT JOIN forum_posts
ON forum_topics.topic_id=forum_posts.topic_num
ORDER BY replies DESC
它给了我一个错误:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'LEFT JOIN forum_posts ON
forum_topics.topic_id=forum_posts.topic_num ORDER BY r' at line 1
这是之前工作的查询:
SELECT * FROM forum_topics WHERE subcat_id = '$subcatid' ORDER BY date DESC
为了回声,我使用:
$getChildCategory = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($get);
if($num == 0){ echo 'No Posts';}
else{
while ($row = mysql_fetch_assoc($get)){
回显时,左连接只得到 1 个结果,但旧连接得到 2 个,这是我所期望的。