【问题标题】:group_concat and SQLSTATE[HY000]: General error: 1111 Invalid use of group functiongroup_concat 和 SQLSTATE[HY000]:一般错误:1111 无效使用组函数
【发布时间】:2015-07-07 14:08:01
【问题描述】:

在 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%'))

谁能解释为什么会发生这种情况以及我该如何解决?

【问题讨论】:

  • 聚合函数条件应该在 HAVING 子句中,而不是在 WHERE 子句中。

标签: sql magento


【解决方案1】:

查询的问题是where 子句中的group_concat()。但是,对于此逻辑,您根本不需要 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

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 2016-06-19
    • 1970-01-01
    • 2012-10-24
    • 2015-11-28
    • 1970-01-01
    • 2018-05-14
    • 2012-10-25
    相关资源
    最近更新 更多