Mysql substring extraction using delimiter

非 Y 不嫁゛ 提交于 2019-11-30 16:54:28

问题


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 mysql functions.

For example :

Table Name: Product
-----------------------------------
item_code  name    colors
-----------------------------------
102        ball     red,yellow,green
104        balloon  yellow,orange,red  

I want to select the colors field and extract the substrings as red,yellow and green as separated by comma.


回答1:


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;



回答2:


Check the use of SPLIT_STR function here at line 64 to 69



来源:https://stackoverflow.com/questions/34992575/mysql-substring-extraction-using-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!