2011-08-31 142 views
0

每次我嘗試運行在phpMyAdmin這個MYSQL發言中,我得到這個錯誤的語法時才: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_comment FROM 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"; 

此外,所有的表都正確命名。所以,這可以排除。

UPDATE:

真棒,謝謝。我在評論中加了引號,額外的周長本來就是一個不是逗號的時期。

+1

「'comments'.total_comments'」缺少報價。在DATE_FORMAT('comments','last_comment'「應該是a。我猜。 – Friek

+0

當面對語法錯誤時,首先要做的就是* simplify *。通過刪除它的一部分來簡化表達式,直到錯誤發生最有可能的是,你刪除的最後一件事是問題 –

+0

@Friek請讓它回答,而不是評論 –

回答

3

DATE_FORMAT()接受兩個參數,而不是三個。 See the manual.

我想,你的意思是

`comments`.`total_comments` 

呢?

1
`comments`,`last_comment` 

應該

`comments`.`last_comment` 

即逗號應該是一個點,否則看起來你正試圖將三個參數傳遞給DATE_FORMAT。