Mysql group_concat of repeated keys and count of repetition of multiple columns in 1 query ( Query Optimization )

前端 未结 3 612
夕颜
夕颜 2021-01-19 03:00

This question is regarding query optimization to avoid multiple call to database via PHP.

So Here is scenario, I have two tables one contains information you can cal

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 03:10

    try this one

    SELECT 
         key1, key2, info1, info2, 
         SUM(Scount) AS serial_count, GROUP_CONCAT(Skey1, ' ', Skey2) AS serial_ids,
         SUM(Pcount) AS product_data_count, GROUP_CONCAT(Pkey1, ' ', Pkey2) AS product_data_ids 
    FROM 
    (
    
       SELECT DISTINCT 
         IF(b.serial  < 10 OR b.product_data IS NOT NULL,a.key1, NULL) AS `key1`,
         IF(b.serial  < 10 OR b.product_data IS NOT NULL,a.key2, NULL) AS `key2`,
         IF(b.serial  < 10 OR b.product_data IS NOT NULL,a.info1, NULL) AS `info1`, 
         IF(b.serial  < 10 OR b.product_data IS NOT NULL,a.info2, NULL) AS `info2`,
         IF(b.serial  < 10,a.key1, NULL) AS `Skey1`,
         IF(b.serial  < 10,a.key2, NULL) AS `Skey2`,
         IF(b.product_data IS NOT NULL,a.key1, NULL) AS `Pkey1`,
         IF(b.product_data IS NOT NULL,a.key2, NULL) AS `Pkey2`,
         IF(b.serial < 10, 1, NULL) AS `Scount`,
         IF(b.product_data IS NOT NULL, 1, NULL) AS `Pcount`
       FROM main_info a INNER JOIN product1 b ON  a.key1 = b.key1 AND a.key2= b.key2
    
       UNION ALL
    
       SELECT DISTINCT
         NULL AS `key1`,
         NULL AS `key2`,
         NULL AS `info1`,
         NULL AS `info2`,
         NULL AS `Skey1`,
         NULL AS `Skey2`,
         NULL AS `Pkey1`,
         NULL AS `Pkey2`,
         IF(serial > 9, 1, NULL) AS `Scount`,
         IF(product_data IS NULL, 1, NULL) AS `Pcount`
       FROM product1 WHERE serial > 9 xor product_data IS NULL
    
    ) AS sub GROUP BY info1,info2
    

    RESULT (data from question)

    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | key1 | key2 | info1 | info2 | serial_count | serial_ids  | product_data_count | product_data_ids |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | NULL | NULL | NULL  | NULL  | 1            | NULL        | NULL               | NULL             |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 1    | 2    | 14    | 92    | 1            | 1 2         | 1                  | 1 2              |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 1    | 3    | 15    | 82    | 3            | 1 3,1 4,1 5 | 3                  | 1 3,1 4,1 5      |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 1    | 1    | 15    | 90    | 1            | 1 1         | 1                  | 1 1              |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    

    RESULT (data from comment)

    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | key1 | key2 | info1 | info2 | serial_count | serial_ids  | product_data_count | product_data_ids |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | NULL | NULL | NULL  | NULL  | 1            | NULL        | 1                  | NULL             |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 1    | 2    | 14    | 92    | 1            | 1 2         | 1                  | 1 2              |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 1    | 3    | 15    | 82    | 3            | 1 3,1 4,1 5 | 3                  | 1 3,1 4,1 5      |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 1    | 1    | 15    | 90    | 1            | 1 1         | 1                  | 1 1              |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 2    | 4    | 16    | 88    | 1            | 2 4         | 1                  | 2 4              |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | 2    | 1    | 17    | 90    | NULL         | NULL        | 3                  | 2 1,2 2,2 3      |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    

    NOTE:

    There is something that I can really understand about the base logic behind the question, so answer mainly base on expected result. Such as if group field (info1 and info2) are null, the other result will always null except for serial_count and product_data_count that can be 1 or null, did you really meant to get that? Notice that this answer use another sub query with UNION ALL to satisfy that.

提交回复
热议问题