0

only_full_group_by 的 mysql 新版本问题

我需要使用 Group by (col_id) 和 order by (date) 以及我在 mysql 中的代码来获取表值,如下所示:

SELECT COUNT(created_date) AS `Rows` , ANY_VALUE(id) 
FROM `table` where `my_id` = 1 AND `status` = '0' 
GROUP BY `id` 
ORDER BY `created_date` DESC

我收到如下错误

SQLSTATE [42000]:语法错误或访问冲突:1055 ORDER BY 子句的表达式#1 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 子句中的列的非聚合列“database.table.created_date”;这与 sql_mode=only_full_group_by 不兼容

有人说解决方案吗?

提前致谢...

4

1 回答 1

0

使用“仅完整分组依据”,order by不能是任意列。在之后order by解析,因此只允许聚合键和聚合列。group by

你可以使用:

SELECT COUNT(created_date) AS `Rows`, ANY_VALUE(id)
FROM `table` 
WHERE `my_id` = 1 AND `status` = '0'
GROUP BY `id`
ORDER BY ANY_VALUE(`created_date`) DESC;

如果我不得不猜测,你实际上想要MAX()

ORDER BY MAX(`created_date`) DESC;
于 2017-10-11T11:58:32.523 回答