SELECT列表不在GROUP BY子句中,并包含非聚合列

时间:2016-01-21 00:27:47

标签: mysql sql aggregate mysql-error-1055

收到以下错误:

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'world.country.Code' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

运行以下查询时:

select countrylanguage.language, country.code, sum(country.population*countrylanguage.percentage/100)
from countrylanguage
join country on countrylanguage.countrycode = country.code
group by countrylanguage.language
order by sum(country.population*countrylanguage.percentage) desc ;

使用MySQL世界测试数据库(http://dev.mysql.com/doc/index-other.html)。不知道为什么会这样。目前正在运行MYSQL 5.7.10。

任何想法??? :o

2 个答案:

答案 0 :(得分:23)

正如@Brian Riley已经说过你应该在你的选择中删除1列

public void writeOrder(PrintWriter pw) {
    pw.println("Order Summary:");
    for (Order order : orders) {
        // NOTE: If possible, I would move this code into Order.toString()
        //       and then use pw.println(order);
        pw.print("    Product Id: ");
        pw.print(order.getProductCode());
        pw.print("    Product Description: ");
        pw.print(order.getProductDesc());
        pw.print("    Order Quantity: ");
        pw.print(order.getQuantity());
        pw.print("    Order Cost: £");
        pw.println(order.getCost());
    }
    pw.print("Total Cost: ");
    pw.print(getTotalCost());
    pw.print("p        Number of Orders: ");
    pw.print(numOfOrders());
    pw.print("     Total Products: ");
    pw.println(numOfProducts());
}

或将其添加到您的分组

select countrylanguage.language ,sum(country.population*countrylanguage.percentage/100)
from countrylanguage
join country on countrylanguage.countrycode = country.code
group by countrylanguage.language
order by sum(country.population*countrylanguage.percentage) desc ;

答案 1 :(得分:1)

country.code不在您的group by语句中,并且不是聚合(包含在聚合函数中)。

https://www.w3schools.com/sql/sql_ref_sqlserver.asp