zoukankan      html  css  js  c++  java
  • 查询表中最新一期资产负债率 ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column

    mysql> select ts_code, max(end_date), debt_to_assets from tushare.fina_indicator group by ts_code;
    ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'tushare.fina_indicator.ann_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

    目标:查询表中最新一期资产负债率

    报错内容:ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column

    解决办法:参考https://blog.csdn.net/free377096858/article/details/91963406

    mysql命令行下执行:

    SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

    依然存在的问题:查询出来的数据是乱的,不是想要的数据。

    select ts_code, max(end_date), debt_to_assets from tushare.fina_indicator group by ts_code;
    这种语句要弃用。

    况且,set global sql_mode的设置重启后又失灵了。mysql8.0.17版本中/etc/my.cnf里并无设置内容存在,不知如何修改。

    问题原因: 

    ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现,否则就会报错,或者这个字段出现在聚合函数里面。 

    在select指定的字段要么就要包含在Group By语句的后面,作为分组的依据;要么就要被包含在聚合函数中。

      

    找原因:

    查看mysql版本命令:select version(); 

    查看sql_model参数命令: 
    SELECT @@GLOBAL.sql_mode;

    SELECT @@SESSION.sql_mode; 

    查看:

     ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
    第一项默认开启ONLY_FULL_GROUP_BY了,导致出现了错误,需要把它的默认关掉。

    解决方法: 
    命令行输入 :

    SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';


    不能输入下面的,不然只能起一次作用。后面还是用不了。

    set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 8.0.17    |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> select @@global.sql_mode;
    +-----------------------------------------------------------------------------------------------------------------------+
    | @@global.sql_mode                                                                                                     |
    +-----------------------------------------------------------------------------------------------------------------------+
    | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
    +-----------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select @@session.sql_mode;
    +-----------------------------------------------------------------------------------------------------------------------+
    | @@session.sql_mode                                                                                                    |
    +-----------------------------------------------------------------------------------------------------------------------+
    | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
    +-----------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> set
        -> sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select @@session.sql_mode;
    +----------------------------------------------------------------------------------------------------+
    | @@session.sql_mode                                                                                 |
    +----------------------------------------------------------------------------------------------------+
    | STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
    +----------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select ts_code, max(end_date), debt_to_assets from tushare.fina_indicator group by ts_code;
    +-----------+---------------+----------------+
    | ts_code   | max(end_date) | debt_to_assets |
    +-----------+---------------+----------------+
    | 000001.SZ | 2019-12-31    |         2.3685 |
    | 000002.SZ | 2019-12-31    |        75.3555 |
    | 000004.SZ | 2019-09-30    |        60.0927 |
    | 000005.SZ | 2019-09-30    |        78.5065 |
    
    
  • 相关阅读:
    Visual C++ 2012/2013的内存溢出检測工具
    MATLAB新手教程
    LCD开发之汉字显示
    支持向量机通俗导论(理解SVM的三层境地)
    类与类之间的简单传值
    hibernate官方新手教程 (转载)
    秒杀多线程第四篇 一个经典的多线程同步问题
    Java的位运算符具体解释实例——与(&)、非(~)、或(|)、异或(^)
    mysql基础:mysql与C结合实例
    php实现字符串的排列(交换)(递归考虑所有情况)
  • 原文地址:https://www.cnblogs.com/ttrrpp/p/12724603.html
Copyright © 2011-2022 走看看