查询表中最新一期资产负债率 ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contain

Posted ttrrpp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询表中最新一期资产负债率 ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contain相关的知识,希望对你有一定的参考价值。

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

 

问题原因: 

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 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 |

 

以上是关于查询表中最新一期资产负债率 ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contain的主要内容,如果未能解决你的问题,请参考以下文章

Laravel:语法错误或访问冲突:1055 错误

Mysql5.7版本ERROR 1055问题

MySQL_group by报错 [ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause]

mysql异常ERROR 1055 (42000)说明

实时离线一体大数据在资产租赁saas服务中使用

docker拉取MySQL(percona)运行GROUP BY语句出现ERROR(1055):42000 如何解决。