MySQL substring extraction using delimiter

后端 未结 3 607
情歌与酒
情歌与酒 2020-11-30 12:07

I want to extract the substrings from a string in MySQL. The string contains multiple substrings separated by commas(\',\'). I need to extract these substrings using any MyS

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 12:49

    A possible duplicate of this: Split value from one field to two

    Unfortunately, MySQL does not feature a split string function. As in the link above indicates there are User-defined Split function.

    A more verbose version to fetch the data can be the following:

    SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(colors, ',', 1), ',', -1) as colorfirst,
           SUBSTRING_INDEX(SUBSTRING_INDEX(colors, ',', 2), ',', -1) as colorsecond
    ....
           SUBSTRING_INDEX(SUBSTRING_INDEX(colors, ',', n), ',', -1) as colornth
      FROM product;
    

提交回复
热议问题