SQL请求错误语法

时间:2018-08-08 09:36:17

标签: php mysql sql mysql-8.0

嘿,我的问题很简单:我有这个请求:

->prepare("SELECT
    CONCAT(YEAR(`p`.`created_account`), '-', MONTH(`p`.`created_account`)) AS `month` 
    FROM
   `profile` AS `p` 
    GROUP BY
    YEAR(`p`.`created_account`), MONTH(`p`.`created_account`)");

我在mysql 5中可以正常工作,但是我升级到mysql 8并知道我有此错误:

  

object(Doctrine \ DBAL \ Exception \ SyntaxErrorException)#707(8){[“” driverException“:” Doctrine \ DBAL \ Exception \ DriverException“:private] =>对象(Doctrine \ DBAL \ Driver \ PDOException)#766 (10){[“” ErrorCode“:” Doctrine \ DBAL \ Driver \ PDOException“:private] => int(1064)[” sqlState“:” Doctrine \ DBAL \ Driver \ PDOException“:private] =>字符串(5) “ 42000” [“消息”:受保护的] =>字符串(282)“ SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册在'FROM profile AS p GROUP BY YEAR(pcreated_account),MONTH(p。'created_a'的第1行附近使用正确的语法” [“ string”:“ Exception”:private] =>字符串(0)“” [“”“:protected] =>字符串(5)” 42000“ [” file“:protected] =>字符串(92)

如果可以的话,我可以同时在两个版本中使用它:

->prepare("SELECT COUNT(`p`.`id`) AS `total` FROM `profile` AS `p` GROUP BY YEAR(`p`.`created_account`), MONTH(`p`.`created_account`)");

当我删除该工作组但不按:/分组时,我有:

  

array(9){[0] => array(1){[“ month”] => string(6)“ 2018-6”} [1] => array(1){[“ month”] =>字符串(6)“ 2018-6”} [2] =>数组(1){[“”“] =>字符串(6)” 2018-7“} [3] =>数组(1){[ “ month”] => string(6)“ 2018-8”} [4] => array(1){[“” month“] => string(6)” 2018-8“} [5] => array( 1){[“”“] =>字符串(6)” 2018-8“} [6] =>数组(1){[”“”“ =>字符串(6)” 2018-8“} [7] => array(1){[“”“] =>字符串(6)” 2018-8“} [8] => array(1){[”“”] => string(6)“ 2018-8” }

那么我该怎么做才能不重复当月?感谢所有尝试回答的人:p

2 个答案:

答案 0 :(得分:0)

似乎您缺少CONCAT()的大括号 你可以尝试:

SELECT CONCAT(YEAR(`p`.`created_account`), '-', MONTH(`p`.`created_account`)) AS `month` FROM `profile` AS `p` GROUP BY YEAR(`p`.`created_account`), MONTH(`p`.`created_account`)

答案 1 :(得分:0)

好吧,我发现对于有相同问题的人来说,这样做是这样的:(似乎mysql v8不允许按年份或月份进行分组,所以就这样做:)

->prepare("SELECT DISTINCT CONCAT(YEAR(`p`.`created_account`), '-', MONTH(`p`.`created_account`)) AS `month` FROM `profile` AS `p` GROUP BY (`created_account`)");