sql >> Database teknologi >  >> RDS >> Mysql

Mysql group_concat af gentagne nøgler og antal gentagelser af flere kolonner i 1 forespørgsel (Query Optimization)

Hvad med at kombinere dine to forespørgsler med et JOIN?

SQL:

SELECT tbl1.key1, tbl1.key2, tbl1.info1, tbl1.info2, tbl1.serial_count, tbl1.serial_ids, tbl2.product_data_count, tbl2.product_data_ids FROM ( select * , count(*) as serial_count,GROUP_CONCAT(key1,' ',key2) as serial_ids from ( SELECT distinct if(b.serial < 10,a.key1,null) AS `key1`, if(b.serial < 10,a.key2,null) AS `key2`, if(b.serial < 10,a.info1,null) AS `info1`, if(b.serial < 10,a.info2,null) AS `info2` FROM main_info a inner join product1 b on a.key1 = b.key1 AND a.key2= b.key2 ) as sub group by info1,info2 ) tbl1 LEFT OUTER JOIN ( select * , count(*) as product_data_count,GROUP_CONCAT(key1,' ',key2) as product_data_ids from ( SELECT distinct if(b.product_data IS NOT NULL,a.key1,null) AS `key1`, if(b.product_data IS NOT NULL,a.key2,null) AS `key2`, if(b.product_data IS NOT NULL,a.info1,null) AS `info1`, if(b.product_data IS NOT NULL,a.info2,null) AS `info2` FROM main_info a inner join product1 b on a.key1 = b.key1 AND a.key2= b.key2 ) as sub group by info1,info2 ) tbl2 ON tbl1.info1 = tbl2.info1 AND tbl1.info2 = tbl2.info2 ORDER BY 3,4 ;

Output:

mysql>  SELECT
    -> tbl1.key1, tbl1.key2, tbl1.info1, tbl1.info2, tbl1.serial_count, tbl1.serial_ids,
    -> tbl2.product_data_count, tbl2.product_data_ids
    ->  FROM
    ->  (
    -> select * , count(*) as serial_count,GROUP_CONCAT(key1,' ',key2) as serial_ids from
    ->  (
    ->  SELECT distinct
    ->  if(b.serial  < 10,a.key1,null) AS `key1`,
    ->  if(b.serial  < 10,a.key2,null) AS `key2`,
    ->  if(b.serial  < 10,a.info1,null) AS `info1`,
    ->          if(b.serial  < 10,a.info2,null) AS `info2`
    ->  FROM main_info a inner join product1 b on  a.key1 = b.key1 AND a.key2= b.key2
    ->  ) as sub group by info1,info2
    ->  ) tbl1
    ->  LEFT OUTER JOIN
    ->  (
    -> select * , count(*) as product_data_count,GROUP_CONCAT(key1,' ',key2) as product_data_ids from
    ->  (
    ->  SELECT distinct
    ->  if(b.product_data IS NOT NULL,a.key1,null) AS `key1`,
    ->  if(b.product_data IS NOT NULL,a.key2,null) AS `key2`,
    ->  if(b.product_data IS NOT NULL,a.info1,null) AS `info1`,
    ->          if(b.product_data IS NOT NULL,a.info2,null) AS `info2`
    ->  FROM main_info a inner join product1 b on  a.key1 = b.key1 AND a.key2= b.key2
    ->  ) as sub group by info1,info2
    ->  ) tbl2
    ->  ON tbl1.info1 = tbl2.info1 AND tbl1.info2 = tbl2.info2
    ->  ORDER BY 3,4
    ->  ;
+------+------+-------+-------+--------------+-------------+--------------------+------------------+
| 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              |
|    2 |    1 |    17 |    90 |            1 | 2 1         |                  3 | 2 2,2 3,2 1      |
+------+------+-------+-------+--------------+-------------+--------------------+------------------+
5 rows in set (0.01 sec)

mysql>  select version();
+-----------------+
| version()       |
+-----------------+
| 10.1.10-MariaDB |
+-----------------+
1 row in set (0.00 sec)
 


  1. Oracle Date kolonne rengøring

  2. Flet to tabeller i én SQL-forespørgsel og gør datoværdierne unikke

  3. Google Maps Gem polygon og punkter i MySQL ved hjælp af PHP

  4. MySQL-forespørgsel for aktuel GMT-tid