【问题标题】:Sql request error syntaxsql请求错误语法
【发布时间】:2019-01-15 12:29:05
【问题描述】:

大家好,我的问题很简单:我有这个要求:

->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]=> object(Doctrine\DBAL\Driver\PDOException)#766 (10) { ["errorCode":"Doctrine\DBAL\Driver\PDOException":private]=> int(1064) ["sqlState":"Doctrine\DBAL\Driver\PDOException":private]=> 字符串(5) "42000" ["message":protected]=> string(282) "SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册在第 1 行的 'FROM profile AS p GROUP BY YEAR(p.created_account)、MONTH(p.`created_a' 附近使用正确的语法" ["string":"Exception":private ]=> string(0) "" ["code":protected]=> string(5) "42000" ["file":protected]=> string(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"] => string(6) "2018-6" } [2]=> array(1) { ["month"]=> string(6) "2018-7" } [3]=> array(1) { [ "月"]=> 字符串(6) "2018-8" } [4]=> 数组(1) { ["月"]=> 字符串(6) "2018-8" } [5]=> 数组( 1) { ["month"]=> string(6) "2018-8" } [6]=> array(1) { ["month"]=> string(6) "2018-8" } [7] => array(1) { ["month"]=> string(6) "2018-8" } [8]=> array(1) { ["month"]=> string(6) "2018-8" } }

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

【问题讨论】:

  • 算上你的()
  • 计数时出现同样的错误
  • 语法不是问题 - 至少不是根据:eversql.com/sql-syntax-check-validator
  • 这是 MySQL 中 ONLY_FULL_GROUP_BY 设置的结果吗
  • 我转储它(结果)

标签: php mysql sql mysql-8.0


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    似乎您缺少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`)
    

    【讨论】:

    • 我忘了把它放在帖子里抱歉,但这不起作用......同样的错误
    • 如果我更改了正在运行但不在 mysql 8 上的版本
    • 嗯。诡异的。我使用 v8 并且我必须做的唯一更改是更改 GROUP BY 子句。 ` SELECT CONCAT(YEAR(p.date_joined), '-', MONTH(p.date_joined)) AS month FROM users AS p GROUP BY date_joined;`
    • 真的很奇怪,因为我更改了版本并且可以工作,然后再次更改为 8.0 并且不工作
    • 当我按有效但未分组的方式删除组时:/就像我有 2018-08 2018-08 2018-09 2018-10 2018-10
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多