错误:1111无效使用组功能

时间:2016-08-05 10:57:13

标签: mysql select group-by

我已经搜索了解决方案,但似乎都需要用HAVING替换WHERE。我不相信这会有同样的问题,我无法弄清楚可能出现的问题。我已经尝试替换我在WHERE子句中有引用和任何聚合函数的代码的不同部分,但我仍然留下相同的错误。以下是我的整个MySQL语句,它提取卖家的统计数据及其在选定时期内的表现。

SELECT users.name AS sellername, users.id AS sellerid, `providers`.`id`, providers.name AS providername, 
COUNT(sales.id) as brutto, COUNT(sale_netto.id) as netto, AVG(sale_quality.grade) as grade, 
SUM(COUNT(sales.id)-COUNT(sale_netto.id)) as diff, (COUNT(sales.id)/COUNT(sales.id)+COUNT(sale_netto.id))*100 AS perc 
FROM `product_regretrules` 
LEFT JOIN `product_categories` ON `product_categories`.`id` = `product_regretrules`.`product_category_id` 
LEFT JOIN `product_product_category` ON `product_product_category`.`product_category_id` = `product_categories`.`id` 
LEFT JOIN `products` ON `products`.`id` = `product_regretrules`.`product_id` 
OR `products`.`id` = `product_product_category.product_id` 
LEFT JOIN `sales` ON `sales`.`product_id` = `products`.`id` 
LEFT JOIN `sale_netto` ON `sale_netto`.`sale_id` = `sales`.`id` 
LEFT JOIN `users` ON `users`.`id` = `sales`.`seller_id` 
LEFT JOIN `providers` ON `providers`.`id` = `products`.`provider_id` 
LEFT JOIN `sale_quality` ON `sale_quality`.`sale_id` = `sales`.`id` 
WHERE `sale_netto`.`upload_id` = 725 
AND `sale_netto`.`id` is not null 
AND `sales`.`sell_date` between 2015-12-04 and 2016-08-06 
AND `product_regretrules`.`deleted_at` is null 
GROUP BY `users.id`, `providers`.`id`

请查看查询的问题,我对如何获得正确的数字/统计数据的答案或评论不感兴趣。

2 个答案:

答案 0 :(得分:0)

尝试将此用于GROUP BY

GROUP BY  users.name, users.id, `providers`.`id`, providers.name

您可能拥有严格的GROUP BY模式。这会导致MySQL遵循ANSI标准,并且在聚合查询中不允许SELECT中的“裸”列。

或者,您可以将SELECT更改为:

SELECT MAX(users.name) AS sellername, users.id AS sellerid,
      `providers`.`id`, MAX(providers.name) AS providername,

编辑:

哦,经过仔细检查,我发现了问题。你有:

SUM(COUNT(sales.id)-COUNT(sale_netto.id)) as diff

您无法嵌套聚合函数。据推测,你打算:

(COUNT(sales.id)-COUNT(sale_netto.id)) as diff

答案 1 :(得分:0)

您无法聚合聚合:SUM(COUNT(sales.id)-COUNT(sale_netto.id))

重写您的查询:

SELECT sellername, sellerid, id, providername, brutto, netto, grade, sum(diff), perc
FROM 
(
SELECT users.name AS sellername, users.id AS sellerid, `providers`.`id`, providers.name AS providername, 
COUNT(sales.id) as brutto, COUNT(sale_netto.id) as netto, AVG(sale_quality.grade) as grade, 
COUNT(sales.id)-COUNT(sale_netto.id) as diff, (COUNT(sales.id)/COUNT(sales.id)+COUNT(sale_netto.id))*100 AS perc 
FROM `product_regretrules` 
LEFT JOIN `product_categories` ON `product_categories`.`id` = `product_regretrules`.`product_category_id` 
LEFT JOIN `product_product_category` ON `product_product_category`.`product_category_id` = `product_categories`.`id` 
LEFT JOIN `products` ON `products`.`id` = `product_regretrules`.`product_id` 
OR `products`.`id` = `product_product_category.product_id` 
LEFT JOIN `sales` ON `sales`.`product_id` = `products`.`id` 
LEFT JOIN `sale_netto` ON `sale_netto`.`sale_id` = `sales`.`id` 
LEFT JOIN `users` ON `users`.`id` = `sales`.`seller_id` 
LEFT JOIN `providers` ON `providers`.`id` = `products`.`provider_id` 
LEFT JOIN `sale_quality` ON `sale_quality`.`sale_id` = `sales`.`id` 
WHERE `sale_netto`.`upload_id` = 725 
AND `sale_netto`.`id` is not null 
AND `sales`.`sell_date` between 2015-12-04 and 2016-08-06 
AND `product_regretrules`.`deleted_at` is null 
GROUP BY `users.id`, `providers`.`id`
) as t
GROUP BY sellername, sellerid, id, providername, brutto, netto, grade, perc;