Mysql 使用have、GROUP BY和GROUP_CONCAT计算SQL

Mysql 使用have、GROUP BY和GROUP_CONCAT计算SQL,mysql,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(DISTIN

如何计算此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,在其中围绕实际select计算行数

因此,在你的情况下:

SELECT COUNT(*) 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)
);
您可以通过以下方式完成:

因此,您只需包装另一个select,在其中围绕实际select计算行数

因此,在你的情况下:

SELECT COUNT(*) 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)
);

一种简单的方法是将整个查询作为一个子查询,用
selectcount(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)
);

一种简单的方法是将整个查询作为一个子查询,用
selectcount(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)
);