What is the opposite of GROUP_CONCAT in MySQL?
Asked Answered
H

6

41

I seem to come against this problem a lot, where I have data that's formatted like this:

+----+----------------------+
| id | colors               |
+----+----------------------+
| 1  | Red,Green,Blue       |
| 2  | Orangered,Periwinkle |
+----+----------------------+

but I want it formatted like this:

+----+------------+
| id | colors     |
+----+------------+
| 1  | Red        |
| 1  | Green      |
| 1  | Blue       |
| 2  | Orangered  |
| 2  | Periwinkle |
+----+------------+

Is there a good way to do this? What is this kind of operation even called?

Heaney answered 25/6, 2013 at 22:30 Comment(3)
That operation is called pivoting / unpivotingBerzelius
You can use FIND_IN_SET dev.mysql.com/doc/refman/8.0/en/… You also could combine in a JOIN.Trimaran
@Berzelius I thought it's called transposition?Bannock
O
14

I think it is what you need (stored procedure) : Mysql split column string into rows

DELIMITER $$

DROP PROCEDURE IF EXISTS explode_table $$
CREATE PROCEDURE explode_table(bound VARCHAR(255))

BEGIN

DECLARE id INT DEFAULT 0;
DECLARE value TEXT;
DECLARE occurance INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE splitted_value INT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT table1.id, table1.value
                                     FROM table1
                                     WHERE table1.value != '';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

DROP TEMPORARY TABLE IF EXISTS table2;
CREATE TEMPORARY TABLE table2(
`id` INT NOT NULL,
`value` VARCHAR(255) NOT NULL
) ENGINE=Memory;

OPEN cur1;
  read_loop: LOOP
    FETCH cur1 INTO id, value;
    IF done THEN
      LEAVE read_loop;
    END IF;

    SET occurance = (SELECT LENGTH(value)
                             - LENGTH(REPLACE(value, bound, ''))
                             +1);
    SET i=1;
    WHILE i <= occurance DO
      SET splitted_value =
      (SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(value, bound, i),
      LENGTH(SUBSTRING_INDEX(value, bound, i - 1)) + 1), ',', ''));

      INSERT INTO table2 VALUES (id, splitted_value);
      SET i = i + 1;

    END WHILE;
  END LOOP;

  SELECT * FROM table2;
 CLOSE cur1;
 END; $$
Overzealous answered 25/6, 2013 at 22:45 Comment(4)
Awesome, that's exactly what I was looking forHeaney
@kmas, What does "stocked procedure" mean?Pinnati
What does the bound argument do? (Edit): looks like it acts as the delimiter to replace. REPLACE(str, find_string, replace_with) => REPLACE(value, bound, '')Katleen
You truly deserve an award for making a procedure and making it soooo easy for everyone to use. You saved my day ,Thank you Thank you so much !!Chamonix
C
27

You could use a query like this:

SELECT
  id,
  SUBSTRING_INDEX(SUBSTRING_INDEX(colors, ',', n.digit+1), ',', -1) color
FROM
  colors
  INNER JOIN
  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) n
  ON LENGTH(REPLACE(colors, ',' , '')) <= LENGTH(colors)-n.digit
ORDER BY
  id,
  n.digit

Please see fiddle here. Please notice that this query will support up to 4 colors for every row, you should update your subquery to return more than 4 numbers (or you should use a table that contains 10 or 100 numbers).

Collywobbles answered 25/6, 2013 at 22:36 Comment(7)
This isn't quite what I'm looking for, I was more looking for something that can handle N rows per id. Thanks though :)Heaney
@JasonHamje if you need to use a query and not a stored procedure, there's no other way :)Collywobbles
Thanks a ton. Used over Here (Edit2 chunk) and gave attribution :pSondrasone
@Sondrasone you're welcome! thanks to you for the attribution! ;)Collywobbles
Nice answer. In the general case, this method is very powerful if combined with the technique from this answer for generating a long sequence of numbers.Distraint
i've total of 30 words by comma seperater, by using the above code, it is not showing all the records, instead showing 3 to 4 words onlyYarvis
@Yarvis yes, you should add more numbers to the select 0 select 1 select 2 etc. to support more than 4 words .. or you should use a join to a numbers tableCollywobbles
O
14

I think it is what you need (stored procedure) : Mysql split column string into rows

DELIMITER $$

DROP PROCEDURE IF EXISTS explode_table $$
CREATE PROCEDURE explode_table(bound VARCHAR(255))

BEGIN

DECLARE id INT DEFAULT 0;
DECLARE value TEXT;
DECLARE occurance INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE splitted_value INT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT table1.id, table1.value
                                     FROM table1
                                     WHERE table1.value != '';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

DROP TEMPORARY TABLE IF EXISTS table2;
CREATE TEMPORARY TABLE table2(
`id` INT NOT NULL,
`value` VARCHAR(255) NOT NULL
) ENGINE=Memory;

OPEN cur1;
  read_loop: LOOP
    FETCH cur1 INTO id, value;
    IF done THEN
      LEAVE read_loop;
    END IF;

    SET occurance = (SELECT LENGTH(value)
                             - LENGTH(REPLACE(value, bound, ''))
                             +1);
    SET i=1;
    WHILE i <= occurance DO
      SET splitted_value =
      (SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(value, bound, i),
      LENGTH(SUBSTRING_INDEX(value, bound, i - 1)) + 1), ',', ''));

      INSERT INTO table2 VALUES (id, splitted_value);
      SET i = i + 1;

    END WHILE;
  END LOOP;

  SELECT * FROM table2;
 CLOSE cur1;
 END; $$
Overzealous answered 25/6, 2013 at 22:45 Comment(4)
Awesome, that's exactly what I was looking forHeaney
@kmas, What does "stocked procedure" mean?Pinnati
What does the bound argument do? (Edit): looks like it acts as the delimiter to replace. REPLACE(str, find_string, replace_with) => REPLACE(value, bound, '')Katleen
You truly deserve an award for making a procedure and making it soooo easy for everyone to use. You saved my day ,Thank you Thank you so much !!Chamonix
E
3

No need for a stored procedure. A CTE is enough:

CREATE TABLE colors(id INT,colors TEXT);
INSERT INTO colors VALUES (1, 'Red,Green,Blue'), (2, 'Orangered,Periwinkle');

WITH RECURSIVE
  unwound AS (
    SELECT *
      FROM colors
    UNION ALL
    SELECT id, regexp_replace(colors, '^[^,]*,', '') colors
      FROM unwound
      WHERE colors LIKE '%,%'
  )
  SELECT id, regexp_replace(colors, ',.*', '') colors
    FROM unwound
    ORDER BY id
;
+------+------------+
| id   | colors     |
+------+------------+
|    1 | Red        |
|    1 | Green      |
|    1 | Blue       |
|    2 | Orangered  |
|    2 | Periwinkle |
+------+------------+
Eparch answered 27/5, 2021 at 17:13 Comment(2)
If only this existed in 2013! So cool. I don't work with MySQL very often anymore but if I do I'll definitely remember to check this out.Heaney
@JasonHamje It's not MySQL/MariaDB-specific. Same code works with PostgreSQL. And if one loads an extension to add the function regexp_replace, it can also be run on SQLite.Eparch
L
2

This saved me many hours! Taking it a step further: On a typical implementation there would in all likelyhood be a table that enumerates the colours against an identitying key, color_list. A new colour can be added to the implementation without having to modify the query and the potentially endless union -clause can be avoided altogether by changing the query to this:

SELECT id,
  SUBSTRING_INDEX(SUBSTRING_INDEX(colors, ',', n.digit+1), ',', -1) color
FROM
  colors
  INNER JOIN
  (select id as digit from color_list) n
  ON LENGTH(REPLACE(colors, ',' , '')) <= LENGTH(colors)-n.digit
ORDER BY id, n.digit;

It is important that the Ids in table color_list remain sequential, however.

Lambert answered 19/6, 2015 at 14:19 Comment(0)
C
0

notice this can be done without creating a temporary table

select id, substring_index(substring_index(genre, ',', n), ',', -1) as genre
from my_table
join 
(SELECT @row := @row + 1 as n FROM 
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t,
(SELECT @row:=0) r) as numbers
  on char_length(genre) 
    - char_length(replace(genre, ',', ''))  >= n - 1
Croft answered 1/3, 2016 at 17:8 Comment(2)
can also add count and group by if u wantCroft
Reading & writing the same user variable in a select statement is undefined behaviour. See the MySQL manual re user variables & assignment.Glare
L
-1

if delimiter is part of data but embedded by double quotes then how can we split it.

Example first,"second,s",third

it should come as first second,s third

Longspur answered 10/10, 2018 at 2:53 Comment(1)
A little late looking through this.. but why not just remove the quote by using replace then do what the answer say?Chrestomathy

© 2022 - 2024 — McMap. All rights reserved.