php - MYSQL语法错误

标签 php mysql sql

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

1064 - 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 'comments,last_comment, '%d/%m/%Y %H:%i:%s') ASlast_comment FROMposts' at line 7

代码:

"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";

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

更新:

太棒了,谢谢。我在评论中添加了引号,多余的周长是句号而不是逗号。

最佳答案

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

我想,你的意思是

`comments`.`total_comments`

代替?

关于php - MYSQL语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7255044/

相关文章:

php - 无法上传媒体,在我的 WordPress 网站上也看不到 GridView ?

php - 使用数组更新数据库

mysql - 更新大量数据、日期(+1 小时)、唯一约束

MYSQL:如果另一列等于其他内容,则计算列

sql - 一个项目中允许的最大数据集数

php - Opencart 禁用 Mysql/Php 错误显示

php - 使用 session_regenerate_id(true) 后 session 终止

web-hosting - PHP 解释器和 PHP 处理程序有什么区别?

php - 无法为 symfony 安装 php-xml

sql - 将我的列转换为 postgresql 中的行而不创建任何扩展