MySQL group by issue:与sql_mode = only_full_group_by不兼容

时间:2016-11-18 12:50:39

标签: mysql

任务:显示每个商店购买的产品最多。
3桌:商店,产品,付款。如果出售给1个商店的产品存在平局,那么选择哪个产品并不重要,只需选择其中一个产品。

我对此查询的group by子句有疑问:

SELECT shop_id, product_id, 
(
  SELECT COUNT(*) 
  FROM payment 
  WHERE product.product_id = payment.product_id
) sold
FROM product
GROUP BY shop_id
HAVING MAX(sold)

在MySQL 5.6或更低版本中,此查询可行。 这将是正确的结果:

shop_id | product_id | sold
1         1            3
2         3            1
3         5            1

但是在5.7上我得到与sql_mode = only_full_group_by不兼容,因为select中的product_id包含非聚合数据。

完整错误消息:

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

所以我认为解决方案是在组中添加product_id,如下所示:

SELECT shop_id, product_id, 
(
  SELECT COUNT(*) 
  FROM payment 
  WHERE product.product_id = payment.product_id
) sold
FROM product
GROUP BY shop_id, product_id
HAVING MAX(sold)

修复了错误,但返回了错误的结果,它不再使商店列独一无二。我明白了:

shop_id | product_id | sold
1         1            3
1         2            4
2         3            1
2         4            1
3         5            1

SQLfiddle使用MySQL 5.6,但让每个人的生活更轻松:http://sqlfiddle.com/#!9/ca12bf9/6

2 个答案:

答案 0 :(得分:3)

您的要求非常复杂。你从MySQL GROUP BY的非标准扩展中获得了有用的结果完全是偶然的。基于该扩展的结果与谈话的驴类似。它的效果并不令人惊讶,它的效果令人惊叹。

这是你必须做的。

(1)按产品和商店总结销售情况。 (http://sqlfiddle.com/#!9/ca12bf9/12/0

                   select product.product_id, product.shop_id, count(*) sale_count
                     from product
                     join payment on product.product_id = payment.product_id
                    group by product.product_id, product.shop_id

(2)通过总结(1)(http://sqlfiddle.com/#!9/ca12bf9/13/0

找出每个商店中畅销产品的销售单位数量
     SELECT MAX(sale_count) max_sale_count, shop_id
       FROM (
                   select product.product_id, product.shop_id, count(*) sale_count
                     from product
                     join payment on product.product_id = payment.product_id
                    group by product.product_id, product.shop_id
                 ) findmax
       GROUP BY shop_id

(3)加入(1)至(2)以检索每个商店中销售最多的产品的标识。 (http://sqlfiddle.com/#!9/ca12bf9/11/0

SELECT a.product_id, a.shop_id, b.max_sale_count
  FROM (
            select product.product_id, product.shop_id, count(*) sale_count
              from product
              join payment on product.product_id = payment.product_id
             group by product.product_id, product.shop_id    
       ) a
  JOIN (
         SELECT MAX(sale_count) max_sale_count, shop_id
                FROM (
                       select product.product_id, product.shop_id, count(*) sale_count
                         from product
                         join payment on product.product_id = payment.product_id
                        group by product.product_id, product.shop_id
                     ) findmax
               GROUP BY shop_id
       ) b ON a.shop_id = b.shop_id AND a.sale_count = b.max_sale_count

您提供的数据有平局。因此,两个不同的产品在您的一家商店中成为最佳卖家。

这种查询将结构化置于结构化查询语言中。

答案 1 :(得分:1)

您可以将sql_mode设置为空白 通过设置onActivityDestroyed

或者您可以使用Location service

使用非聚合列与group by永远不会保证结果是什么。

https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value

更新1 http://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html