douye4254 2011-08-31 09:28
浏览 104
已采纳

MYSQL语法错误

Every time I try to run this MYSQL statement in phpMyAdmin I get this sytax error:

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

Code:

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

Also, all of the tables are named correctly. So, that can be ruled out.

UPDATE:

Awesome, thanks. I added the quote on comments and the extra perimeter was meant to be a period not a comma.

  • 写回答

2条回答 默认 最新

  • doukuipai8544 2011-08-31 09:30
    关注

    DATE_FORMAT() accepts two parameters, not three. See the manual.

    I suppose, you meant

    `comments`.`total_comments`
    

    instead?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流