mysql - 使用 HAVING、GROUP BY 和 GROUP_CONCAT 对 SQL 进行计数

标签 mysql sql

计算此 SQL 中的行数的查询应该是什么样子。我需要的是 SQL 查询而不是 PHP 函数。

SELECT
`main_table`.`entity_id`,
`main_table`.`supplier_id`,
`main_table`.`exported_at`,
`main_table`.`created_at`,
`main_table`.`updated_at`,
`main_table`.`product_id`,
`main_table`.`external_id`,
GROUP_CONCAT(DISTINCT
           (IF(attr.alias = 'attribute_set_name', attr_item.value, NULL)))                  AS `attribute_attribute_set_name`,
GROUP_CONCAT(DISTINCT (IF(attr.alias = 'title', attr_item.value, NULL)))                      AS `attribute_title`,
GROUP_CONCAT(DISTINCT (IF(attr.alias = 'normal_price', attr_item.value, NULL)))               AS `attribute_normal_price`,
(SELECT SUM(IF(attr_item.value = '', 1, 0)) = 0
FROM divante_integration_stock_attribute attr INNER JOIN divante_integration_stock_attribute_item AS attr_item
   ON attr_item.attribute_id = attr.entity_id
WHERE is_required = 1 AND attr_item.item_id = main_table.entity_id)                          AS `is_complete`,
(SELECT GROUP_CONCAT(attr.code)
FROM divante_integration_stock_attribute attr INNER JOIN divante_integration_stock_attribute_item AS attr_item
   ON attr_item.attribute_id = attr.entity_id
WHERE is_required = 1 AND attr_item.item_id = main_table.entity_id AND attr_item.value = '') AS `not_complete_attributes`,
`main_table`.`entity_id`,
`cp`.`sku`
FROM `divante_integration_stock` AS `main_table` LEFT JOIN `divante_integration_stock_attribute_item` AS `attr_item`
ON attr_item.item_id = main_table.entity_id
LEFT JOIN `divante_integration_stock_attribute` AS `attr` ON attr.entity_id = attr_item.attribute_id
LEFT JOIN `catalog_product_entity` AS `cp` ON main_table.product_id = cp.entity_id
GROUP BY `main_table`.`entity_id`
HAVING (GROUP_CONCAT(DISTINCT (IF(attr.alias = 'attribute_set_name', attr_item.value, NULL))) LIKE '%ebook%') AND
   (GROUP_CONCAT(DISTINCT (IF(attr.alias = 'title', attr_item.value, NULL))) LIKE '%sieci%') AND
   (GROUP_CONCAT(DISTINCT (IF(attr.alias = 'normal_price', CAST(attr_item.value AS DECIMAL(12, 4)), NULL))) BETWEEN 40 AND 999999.9999)

最佳答案

一种简单的方法是将整个查询作为子查询,并用 SELECT Count(1) FROM (); 包装::

SELECT Count(1)
FROM
(
    SELECT
    `main_table`.`entity_id`,
    `main_table`.`supplier_id`,
    `main_table`.`exported_at`,
    `main_table`.`created_at`,
    `main_table`.`updated_at`,
    `main_table`.`product_id`,
    `main_table`.`external_id`,
    GROUP_CONCAT(DISTINCT
               (IF(attr.alias = 'attribute_set_name', attr_item.value, NULL)))                  AS `attribute_attribute_set_name`,
    GROUP_CONCAT(DISTINCT (IF(attr.alias = 'title', attr_item.value, NULL)))                      AS `attribute_title`,
    GROUP_CONCAT(DISTINCT (IF(attr.alias = 'normal_price', attr_item.value, NULL)))               AS `attribute_normal_price`,
    (SELECT SUM(IF(attr_item.value = '', 1, 0)) = 0
    FROM divante_integration_stock_attribute attr INNER JOIN divante_integration_stock_attribute_item AS attr_item
       ON attr_item.attribute_id = attr.entity_id
    WHERE is_required = 1 AND attr_item.item_id = main_table.entity_id)                          AS `is_complete`,
    (SELECT GROUP_CONCAT(attr.code)
    FROM divante_integration_stock_attribute attr INNER JOIN divante_integration_stock_attribute_item AS attr_item
       ON attr_item.attribute_id = attr.entity_id
    WHERE is_required = 1 AND attr_item.item_id = main_table.entity_id AND attr_item.value = '') AS `not_complete_attributes`,
    `main_table`.`entity_id`,
    `cp`.`sku`
    FROM `divante_integration_stock` AS `main_table` LEFT JOIN `divante_integration_stock_attribute_item` AS `attr_item`
    ON attr_item.item_id = main_table.entity_id
    LEFT JOIN `divante_integration_stock_attribute` AS `attr` ON attr.entity_id = attr_item.attribute_id
    LEFT JOIN `catalog_product_entity` AS `cp` ON main_table.product_id = cp.entity_id
    GROUP BY `main_table`.`entity_id`
    HAVING (GROUP_CONCAT(DISTINCT (IF(attr.alias = 'attribute_set_name', attr_item.value, NULL))) LIKE '%ebook%') AND
       (GROUP_CONCAT(DISTINCT (IF(attr.alias = 'title', attr_item.value, NULL))) LIKE '%sieci%') AND
       (GROUP_CONCAT(DISTINCT (IF(attr.alias = 'normal_price', CAST(attr_item.value AS DECIMAL(12, 4)), NULL))) BETWEEN 40 AND 999999.9999)
);

关于mysql - 使用 HAVING、GROUP BY 和 GROUP_CONCAT 对 SQL 进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37509591/

相关文章:

Mysql,更新具有多条记录的列

使用 mysql 的书籍数据库的 php 搜索脚本

MYSQL - 如何获取每条记录的最大值?

mysql - 将此查询分成批处理

php - Laravel SQL 按 WHERE IN Group By 更新行

mysql - 缺少 END - Mysql 存储过程

php - 如何使用 PDO 查找数据库表的最后一条记录?

SQL四舍五入没有小数点的整数

sql - Oracle查询显示同一列中的2个总和字段并根据不同表中的不同字段条件进行计算

mysql - 为多个产品属性创建查询