mysql5.7升级问题GROUP BY不兼容sql_mode=only_full_group_by
Posted
技术标签:
【中文标题】mysql5.7升级问题GROUP BY不兼容sql_mode=only_full_group_by【英文标题】:mysql5.7 upgrade issue GROUP BY incompatible with sql_mode=only_full_group_by 【发布时间】:2016-10-03 19:37:15 【问题描述】:我将我的服务器升级到 mysql5.7,我在这个请求上遇到了这个问题:
错误号:1055
SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含 非聚合列“vuillermoz.pi.id_pierre”不是 功能上依赖于 GROUP BY 子句中的列;这是 与 sql_mode=only_full_group_by 不兼容
SELECT `pi`.`id_pierre`,
`pi`.`couleur_nom`,
`pi`.`couleur_code`,
`pi`.`image`,
count(p.id_prod) AS countproduits
FROM (`produit` p)
LEFT JOIN `categorie` c1_4
ON `p`.`afficher` = `p`.`afficher`
AND p.rid_cat = c1_4.id_cat
AND c1_4.afficher = 1
LEFT OUTER JOIN `categorie` c1_3
ON `c1_4`.`rid_cat` = `c1_3`.`id_cat`
AND c1_3.afficher = '1'
LEFT OUTER JOIN `categorie` c1_2
ON `c1_3`.`rid_cat` = `c1_2`.`id_cat`
AND c1_2.afficher = '1'
LEFT OUTER JOIN `categorie` c1_1
ON `c1_2`.`rid_cat` = `c1_1`.`id_cat`
AND c1_1.afficher = '1'
LEFT OUTER JOIN `type` t
ON `p`.`rid_type` = `t`.`id_type`
INNER JOIN `pierre` pi
ON `p`.`rid_pierre` = `pi`.`id_pierre`
LEFT OUTER JOIN `matiere` ma
ON `p`.`rid_matiere` = `ma`.`id_matiere`
LEFT OUTER JOIN `couleur` co
ON `p`.`rid_couleur` = `co`.`id_couleur`
WHERE `rid_pierre` <> 20
AND `p`.`afficher` = 1
AND (
p.id_prod IN
(
SELECT rid_prod
FROM produit__sexe
WHERE rid_sexe IN ("1")))
AND (
p.id_prod IN
(
SELECT rid_prod
FROM produit__decli
WHERE stock > "0")
OR p.disposurcommande = "1")
GROUP BY `pi`.`couleur_nom`
ORDER BY `couleur_nom` ASC
我可以修复请求而不是更改 sql_mode 参数吗? 最好的问候
【问题讨论】:
你需要为这个很难阅读的问题添加一些格式 MySQL Group By functionality in different version的可能重复 【参考方案1】:试试这个
mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
【讨论】:
【参考方案2】:通常,MySQL 允许在 SELECT 语句中包含非聚合列 未在 GROUP 中命名的输出列列表或 HAVING 子句 BY 子句。例如: SELECT a, b, COUNT() FROM t GROUP BY a; ONLY_FULL_GROUP_BY 标志需要非聚合输出列(或 HAVING 列)在 GROUP BY 中命名: SELECT a, b, COUNT() FROM t GROUP BY a, b;
【讨论】:
以上是关于mysql5.7升级问题GROUP BY不兼容sql_mode=only_full_group_by的主要内容,如果未能解决你的问题,请参考以下文章
升级到mysql8.0后部分SQL语句出现group by不兼容问题
解决Mysql5.7以上版本, 使用group by抛出Expression #1 of SELECT list is not in GROUP BY clause and contains no异常