Don't store comma separated lists of values. (This doesn't mean to use semicolons in place of commas.)
SQL Antipatterns: Avoiding the Pitfalls of Database Programming -- Bill Karwin, Chapter 2
Available at Amazon and other fine booksellers.
https://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
For a finite number of values, we can split those out into separate rows
(Replace inline view t
with your table)
SELECT t.id
, i.n
, NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT( t.room_type ,';;'),';',i.n),';',-1),'') AS room_type_n
, NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT( t.room_size ,';;'),';',i.n),';',-1),'') AS room_size_n
FROM ( SELECT 1 AS id, 'a;b;c' AS room_type, '5;7;12' AS room_size
UNION ALL SELECT 2,'b;c;d;f','8;2;4'
UNION ALL SELECT 3,'b;c;d;f;g;h','6;4;5;6;7;2;9'
) t
CROSS
JOIN ( SELECT 1 AS n
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) i
HAVING NOT ( room_type_n <=> NULL )
ORDER BY t.id, i.n
Returns:
id n room_type_n room_size_n
--- --- ----------- -----------
1 1 a 5
1 2 b 7
1 3 c 12
2 1 b 8
2 2 c 2
2 3 d 4
2 4 f (NULL)
3 1 b 6
3 2 c 4
3 3 d 5
3 4 f 6
3 5 g 7
3 6 h 2
Inline view i
should be extended to the finite, maximum number of values we expect to extract: UNION ALL SELECT 10 UNION ALL SELECT 11 ...
.