如何解决 SELECT 列表不在 GROUP BY 子句中且包含非聚合?
How to resolve SELECT list is not in GROUP BY clause and contains nonaggregated?
关于此请求,我在 MYSQL 5.7 中收到错误。如何解决此错误?
#1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.c.customers_group_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select SQL_CALC_FOUND_ROWS c.customers_firstname,
c.customers_lastname,
c.customers_group_id,
sum(op.products_quantity * op.final_price) as ordersum
from customers c,
orders_products op,
orders o
where c.customers_id = o.customers_id
and o.orders_id = op.orders_id
group by c.customers_firstname,
c.customers_lastname
order by ordersum DESC
在 group by 子句中也包含 c.customers_group_id
您在 GROUP BY
子句中遗漏了 c.customers_group_id
。
您可以使用 ANSI JOIN
模式代替 旧式逗号分隔表列表 模式。
以下代码适用于您的情况:
SELECT SQL_CALC_FOUND_ROWS c.customers_firstname,
c.customers_lastname,
c.customers_group_id,
sum(op.products_quantity * op.final_price) as ordersum
FROM customers c
JOIN orders o ON o.customers_id = c.customers_id
JOIN orders_products op ON op.orders_id = o.orders_id
GROUP BY c.customers_firstname,
c.customers_lastname,
c.customers_group_id -- you missed this
ORDER BY ordersum DESC
关于此请求,我在 MYSQL 5.7 中收到错误。如何解决此错误?
#1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.c.customers_group_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select SQL_CALC_FOUND_ROWS c.customers_firstname,
c.customers_lastname,
c.customers_group_id,
sum(op.products_quantity * op.final_price) as ordersum
from customers c,
orders_products op,
orders o
where c.customers_id = o.customers_id
and o.orders_id = op.orders_id
group by c.customers_firstname,
c.customers_lastname
order by ordersum DESC
在 group by 子句中也包含 c.customers_group_id
您在 GROUP BY
子句中遗漏了 c.customers_group_id
。
您可以使用 ANSI JOIN
模式代替 旧式逗号分隔表列表 模式。
以下代码适用于您的情况:
SELECT SQL_CALC_FOUND_ROWS c.customers_firstname,
c.customers_lastname,
c.customers_group_id,
sum(op.products_quantity * op.final_price) as ordersum
FROM customers c
JOIN orders o ON o.customers_id = c.customers_id
JOIN orders_products op ON op.orders_id = o.orders_id
GROUP BY c.customers_firstname,
c.customers_lastname,
c.customers_group_id -- you missed this
ORDER BY ordersum DESC