每次我尝试在 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') AS
last_commentFROM
posts' 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";
此外,所有表的名称都正确。所以,这可以排除。
更新:
太棒了,谢谢。我在评论中添加了引号,多余的周长是句号而不是逗号。
最佳答案
关于php - MYSQL语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7255044/