MySQL错误Reatest-n-per-group

时间:2012-09-23 20:42:47

标签: php greatest-n-per-group

救救我!

       SELECT ma_forum.*, ma_forum_cat.*

        FROM ma_forum, ma_forum_cat

        JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id

        GROUP BY ma_forum.not_id

        WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."' 

        AND ma_forum.not_status='Ativo'

        ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC

“您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在'WHERE ma_forum.notcat_id = ma_forum_cat.notcat_id AND ma_forum.notcat_id ='1''附近使用正确的语法9"

1 个答案:

答案 0 :(得分:0)

您的查询顺序错误; GROUP BY应该在WHERE之后和ORDER BY之前:

SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."' 
AND ma_forum.not_status='Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC

我不知道你正在使用什么数据库,但这里有MySQL's SELECT syntax的链接