Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我认为SQL语法错误与左连接有关?_Php_Mysql_Database - Fatal编程技术网

Php 我认为SQL语法错误与左连接有关?

Php 我认为SQL语法错误与左连接有关?,php,mysql,database,Php,Mysql,Database,我收到这个mysql错误,我不知道为什么。我正在学习一个教程,我已经逐字逐句地从视频中复制了语法。有人能看出哪里出了问题吗 以下是错误: 您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解要使用的接近“左连接”的正确语法(选择post\u id,在第11行将(comment\u id)计数为“total\u co” $query = "SELECT `posts`.`post_id` AS `id`,

我收到这个mysql错误,我不知道为什么。我正在学习一个教程,我已经逐字逐句地从视频中复制了语法。有人能看出哪里出了问题吗

以下是错误:

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解要使用的接近“左连接”的正确语法(选择
post\u id
,在第11行将(
comment\u id
)计数为“total\u 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";

非常感谢您的帮助。谢谢。

删除第10行的逗号

FROM `posts`
     LEFT JOIN 
正确的语法是:

FROM  
    <table a>
  LEFT JOIN  
    <table b>
      ON  <join condition>
  • 您还可以添加一个
    (post\u id,comment\u date)
    索引到表
    comments

  • 在主
    选择
    列表中,如果对于没有评论的帖子,您更希望
    0
    s而不是
    NULL
    s,请将
    comments.total_comments,
    更改为:

            COUNT(*) AS total_comments,
    
    COALESCE(comments.total_comments, 0) AS total_comments,
    

  • 删除第10行的逗号

    FROM `posts`
         LEFT JOIN 
    
    正确的语法是:

    FROM  
        <table a>
      LEFT JOIN  
        <table b>
          ON  <join condition>
    
  • 您还可以添加一个
    (post\u id,comment\u date)
    索引到表
    comments

  • 在主
    选择
    列表中,如果对于没有评论的帖子,您更希望
    0
    s而不是
    NULL
    s,请将
    comments.total_comments,
    更改为:

            COUNT(*) AS total_comments,
    
    COALESCE(comments.total_comments, 0) AS total_comments,
    

  • 哇,谢谢!我不知道为什么我没有发现。也感谢Danny尝试帮助。ypercube谢谢你提供的额外信息。我是php/mysql新手,所以这很有帮助!哇,谢谢!我不知道为什么我没有发现。也感谢Danny尝试帮助。ypercube谢谢你提供的额外信息。我是新来的php/mysql,所以这很有帮助!