1

この mysql エラーが発生しましたが、その理由がわかりません。私はチュートリアルに従っており、ビデオの構文を単語ごとにコピーしました。誰が何が間違っているかを見つけることができますか?

エラーは次のとおりです。

SQL 構文にエラーがあります。MySQL サーバーのバージョンに対応するマニュアルで、11 行目の 'LEFT JOIN ( SELECT post_id, COUNT( comment_id) AS `total_co''付近で使用する正しい構文を確認してください。

     $query = "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";

あなたの助けに感謝します。ありがとうございました。

4

1 に答える 1

6

,10 行目のコンマを削除します。

FROM `posts`
     LEFT JOIN 

正しい構文は次のとおりです。

FROM  
    <table a>
  LEFT JOIN  
    <table b>
      ON  <join condition>

問題とは無関係:

  • 効率を (わずかに) 改善するには、 を次のように変更COUNT(comment_id)します。

            COUNT(*) AS total_comments,
    
  • (post_id, comment_date)テーブルにインデックスを追加することもできますcomments

  • メインSELECTリストで、コメントのない投稿0よりもを使用したい場合は、次のように変更します。NULLcomments.total_comments,

    COALESCE(comments.total_comments, 0) AS total_comments,
    
于 2012-07-01T23:26:08.540 に答える