group by 多个字段,应该到哪个字段为止?
--------所有场景下-所有规则-所有表达式
select rules.scene_no,rules.scene_name,e.rule_id,rules.name rule_name,rules.sort rule_sort,
e.id expression_id,e.name expression_name,e.expression
from ry_risk_rules rules
left join ry_risk_expression e on e.rule_id=rules.id
where rules.enabled=1 and rules.is_delete=0 and e.is_delete=0
group by rules.scene_no,e.rule_id,expression_id;
当你想看到的最后分组结果是在表达式上,那么一定要group by表达式。否则若只到group by e.rule_id,结果就只会显示rule_id下的第一条表达式记录,而不是该rule_id下的所有表达式。
====================================================================================================
报错:
Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'roeondb.e.id' which is ……
#报错:Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'roeondb.e.id' which is ……
#执行以下个命令,可以查看 sql_mode 的内容。
mysql> SHOW SESSION VARIABLES;
mysql> SHOW GLOBAL VARIABLES;
mysql> select @@sql_mode;
#可见session和global 的sql_mode的值都为:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#解决:执行以下两个命令
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';
#这两个命令,去掉 sql_mode 的 ONLY_FULL_GROUP_BY
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';
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';(亲测有效)
方法来源网址链接:
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo. - 星朝 - 博客园 https://www.cnblogs.com/jpfss/p/10401753.html