-4

嘿伙计们,我的问题很简单:我有这个要求:

->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]=> string(5) "42000" ["message":protected]=> string(282) "SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册以获取正确的语法使用'FROM profileAS pGROUP BY YEAR( p. created_account), MONTH( p.`created_a' at line 1" ["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) { ["month" ]=> string(6) "2018-8" } [4]=> array(1) { ["month"]=> string(6) "2018-8" } [5]=> array(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

4

2 回答 2

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`)
于 2018-08-08T09:42:31.863 回答
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`)");
于 2018-08-08T12:37:05.610 回答