[err] 1055 - expression #1 of order by clause is not in group by clause and
时间: 2024-09-24 21:07:05 浏览: 62
错误1055通常在SQL查询中出现,这意味着你在ORDER BY子句中引用了一个未包含在GROUP BY子句中的表达式。在SQL中,GROUP BY子句用于对数据集按照一列或多列进行分组,而ORDER BY则确定如何对每个组内的行进行排序。如果你试图对GROUP BY之外的列进行排序,数据库将无法确定每一组应该使用哪一行,因为每一组可能有多个匹配的值。
例如:
```sql
SELECT column1, AVG(column2)
FROM table
GROUP BY column1
ORDER BY column3; -- 错误,column3未在GROUP BY中
```
在这种情况下,你需要把`column3`添加到GROUP BY子句中,如果它是你想要基于其排序的结果,或者修改ORDER BY部分,只使用GROUP BY列进行排序:
```sql
SELECT column1, AVG(column2)
FROM table
GROUP BY column1, column3
ORDER BY column1, column3;
```
或者
```sql
SELECT column1, AVG(column2)
FROM table
GROUP BY column1
ORDER BY AVG(column2);
```
相关问题
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
、和[3]引用的内容都是关于MySQL报错1055的问题。这个错误是由于在使用ORDER BY子句时,ORDER BY子句中的列没有在GROUP BY子句中,并且包含了不是在GROUP BY子句中的列,这与sql_mode=only_full_group_by选项不兼容所导致的。换句话说,这个错误是由于使用了不合法的ORDER BY子句。
要解决这个问题,可以有以下几种方法:
1. 修改SQL查询语句,确保ORDER BY子句中的列都在GROUP BY子句中,并且不包含不在GROUP BY子句中的列。这样可以确保查询语句符合sql_mode=only_full_group_by选项的要求。
2. 修改MySQL配置文件,将sql_mode选项中的only_full_group_by选项去掉。这样可以关闭只允许全面GROUP BY的模式,但需要注意这可能会导致其他潜在的问题。
3. 升级MySQL版本到5.7.5或更高的版本。从MySQL 5.7.5开始,sql_mode=only_full_group_by成为默认设置,不再允许ORDER BY子句中的非聚合列。
请根据具体情况选择适合的解决方法来解决[Err] 1055的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [MySQL异常:[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona...](https://download.csdn.net/download/weixin_38742656/13685329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated...](https://blog.csdn.net/miachen520/article/details/122390084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains](https://blog.csdn.net/qq_43514711/article/details/122041247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
这是一个MySQL查询错误,错误信息为“[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by”。根据错误信息,这个错误是由于在ORDER BY子句中的列不在GROUP BY子句中,并且包含了非聚合的列。解决这个错误的方法是修改查询语句,将ORDER BY中的列添加到GROUP BY子句中或者使用聚合函数对ORDER BY列进行聚合。
阅读全文