sql语句分组报错是什么原因

img


第一个报错,这个为什么报错?

报错信息如下:
SELECT p.name ,sum(s.price * s.quantity) amount from products p left join sales s on p.id=s.product_id group by s.product_id ORDER BY amount desc limit 3

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.p.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
时间: 0s

SELECT p.name ,sum(s.price * s.quantity) amount from products p left join sales s on p.id=s.product_id group by s.product_id ORDER BY amount desc limit 3查询中,你只按照sales表中的product_id进行了分组,而没有按照products表中的id进行分组,因此在SELECT语句中的p.name列不是聚合列,而是非聚合列,这就导致了错误。
而在SELECT p.name ,sum(s.price * s.quantity) amount from products p left join sales s on p.id=s.product_id group by p.id ORDER BY amount desc limit 3查询中,你按照products表中的id进行了分组,因此p.name列是聚合列,不会导致错误。
如果你想在SELECT p.name ,sum(s.price * s.quantity) amount from products p left join sales s on p.id=s.product_id group by s.product_id ORDER BY amount desc limit 3查询中使用p.name列,可以将它添加到GROUP BY子句中,像这样:
SELECT p.name, SUM(s.price * s.quantity) amount 
FROM products p 
LEFT JOIN sales s ON p.id=s.product_id 
GROUP BY s.product_id, p.name 
ORDER BY amount DESC 
LIMIT 3;

group by后面分组的字段必须出现在select中

  • 建议你看下这篇博客👉 :dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
  • 除此之外, 这篇博客: which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mod中的 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by" 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  •  

    MySQL查询、设置配置:
    
    查看参数:SHOW VARIABLES;
    SHOW VARIABLES WHERE variable_name like '%sql_mode%';
    
    设置参数:SET GLOBAL 参数名称=value;
    如设置数据库最大连接数为:SET GLOBAL max_connections=1000。
    方案1、
    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';
    
    在这里设置的值,是通过select @@sql_mode得到的结果,去掉ONLY_FULL_GROUP_BY得到的。以自己的配置为主,只要去掉ONLY_FULL_GROUP_BY就好。
    方案2、
    通过更改my.cnf实现。本质上和2是一样的,都是关闭ONLY_FULL_GROUP_BY模式。我是通过yum安装的mysql,所以直接编辑/etc/my.cnf,在文件的最后加上
    
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    
    
    然后通过
    
    ystemctl restart mysqld.service
    
    重启数据库。

     

  • 以下回答由chatgpt基于相关博客总结生成:

    这个错误提示的含义是,在执行分组操作的SQL语句中,SELECT语句中的第一列没有包含在GROUP BY子句中,并且该列包含了非聚合的列,在使用sql_mode=only_full_group_by时,这不兼容。 解决该问题的方法有两个: 方案1: 通过设置sql_mode参数来解决该问题。具体步骤为: 1. 查询当前MySQL的sql_mode参数,使用命令:SHOW VARIABLES WHERE variable_name like '%sql_mode%'; 2. 设置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'; 3. 重启MySQL服务,使用命令:systemctl restart mysqld.service。

    方案2: 通过更改my.cnf文件来解决该问题。具体步骤为: 1. 编辑my.cnf文件,使用命令:vi /etc/my.cnf; 2. 在文件的末尾添加sql_mode参数,值为不包含ONLY_FULL_GROUP_BY的值,如:sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION; 3. 保存并退出编辑器; 4. 重启MySQL服务,使用命令:systemctl restart mysqld.service。