Skip to content

Instantly share code, notes, and snippets.

@jadwigo
Last active January 15, 2020 20:19
Show Gist options
  • Save jadwigo/4d556a82dd979a221731b1f960a8c04d to your computer and use it in GitHub Desktop.
Save jadwigo/4d556a82dd979a221731b1f960a8c04d to your computer and use it in GitHub Desktop.
Mysql split
-- https://stackoverflow.com/a/22669310/1531963
-- Assuming that the comma separated list is in table data.list,
-- and it contains listing of codes from other table classification.code, you can do something like:
SELECT
d.id, d.list, c.code
FROM
classification c
JOIN data d
ON d.list REGEXP CONCAT('[[:<:]]', c.code, '[[:>:]]');
Found at https://stackoverflow.com/a/22669310/1531963
So if you have tables and data like this:
CLASSIFICATION (code varchar(4) unique): ('A'), ('B'), ('C'), ('D')
MY_DATA (id int, list varchar(255)): (100, 'C,A,B'), (150, 'B,A,D'), (200,'B')
above SELECT will return
(100, 'C,A,B', 'A'),
(100, 'C,A,B', 'B'),
(100, 'C,A,B', 'C'),
(150, 'B,A,D', 'A'),
(150, 'B,A,D', 'B'),
(150, 'B,A,D', 'D'),
(200, 'B', 'B'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment