It saved me many hours! Let's take one more step: with a typical implementation, there will probably be a table that lists the colors against the identification key, color_list . A new color can be added to the implementation without having to change the request, and a potentially infinite union -clause can be avoided altogether by changing the request 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 identifiers in the color_list table remain consistent.
gerrit_hoekstra Jun 19 '15 at 14:19 2015-06-19 14:19
source share