MYSQL语法错误

时间:2011-08-31 09:28:10

标签: php mysql sql

每当我尝试在phpMyAdmin中运行此MYSQL语句时,我都会收到此sytax错误:

  

1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在第7行的“评论, last_comment , '%d/%m/%Y %H:%i:%s') AS last_comment FROM帖子附近”

代码:

"SELECT
                `posts`.`post_id` AS `id`,
                `posts`.`post_title` AS `title`,
                LEFT(`posts`.`post_body`, 512) AS `preview`,
                `posts`.`post_user` AS `user`,
                DATE_FORMAT(`posts`.`post_date`,'%d/%m/%Y %H:%i:%s') AS `date`,
                `comments`.`total_comments`,
                DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
            FROM `posts`
            LEFT JOIN (
            SELECT
                `post_id`,
                COUNT(`comment_id`) AS `total_comments`,
                MAX(`comment_date`) AS `last_comment`
            FROM `comments`
            GROUP BY `post_id`  
            ) AS `comments`
            ON `posts`.`post_id` = `comments`.`post_id`
            ORDER BY `posts`.`post_date` DESC";

此外,所有表都正确命名。所以,这可以排除。

更新

太棒了,谢谢。我在评论中添加了引用,额外的周长是一段时间而不是逗号。

2 个答案:

答案 0 :(得分:3)

DATE_FORMAT()接受两个参数,而不是三个。 See the manual.

我想,你的意思是

`comments`.`total_comments`

代替?

答案 1 :(得分:1)

`comments`,`last_comment`

应该是

`comments`.`last_comment`

即。逗号应该是一个点,否则看起来你试图将三个参数传递给DATE_FORMAT。