group_concat和SQLSTATE [HY000]:常规错误:1111无效使用组功能

时间:2015-07-07 14:08:01

标签: sql magento

在Magento的销售订单管理页面中,我有时(但并非总是)在页面上收到错误,并在var/report中看到:

  

SQLSTATE [HY000]:常规错误:1111无效使用组功能

SELECT COUNT(DISTINCT main_table.entity_id) FROM `m_sales_flat_order_grid` AS `main_table`

INNER JOIN `m_sales_flat_order_item` 
ON `m_sales_flat_order_item`.order_id=`main_table`.entity_id

INNER JOIN `m_catalog_product_entity_varchar` 
ON (m_catalog_product_entity_varchar.entity_id = `m_sales_flat_order_item`.`product_id`) 
AND `m_catalog_product_entity_varchar`.attribute_id=163 
WHERE (`m_sales_flat_order_item`.parent_item_id IS NULL) 
AND ((group_concat(`m_catalog_product_entity_varchar`.value SEPARATOR ', ') like '%tenzen%'))

任何人都可以阐明为什么会发生这种情况以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

查询的问题是group_concat()子句中的where。但是,对于此逻辑,您根本不需要group_concat()

但是,您需要聚合以将给定实体的所有行组合在一起。这需要一个子查询,所以我建议将其重写为:

SELECT COUNT(*)
FROM (SELECT main_table.entity_id
      FROM `m_sales_flat_order_grid` AS `main_table` INNER JOIN 
           `m_sales_flat_order_item` 
           ON `m_sales_flat_order_item`.order_id = `main_table`.entity_id INNER JOIN 
           `m_catalog_product_entity_varchar` 
           ON m_catalog_product_entity_varchar.entity_id = `m_sales_flat_order_item`.`product_id` AND 
             `m_catalog_product_entity_varchar`.attribute_id = 163 
      WHERE `m_sales_flat_order_item`.parent_item_id IS NULL
      GROUP BY main_table.entity_id
      HAVING SUM(`m_catalog_product_entity_varchar`.value like '%tenzen%') > 0
     ) t