Can Mysql Split a column?

前端 未结 7 1621
别那么骄傲
别那么骄傲 2020-11-22 03:00

I have a column that has comma separated data:

1,2,3
3,2,1
4,5,6
5,5,5

I\'m trying to run a search that would query each value of the CSV s

7条回答
  •  野性不改
    2020-11-22 03:14

    Here is another variant I posted on related question. The REGEX check to see if you are out of bounds is useful, so for a table column you would put it in the where clause.

    SET @Array = 'one,two,three,four';
    SET @ArrayIndex = 2;
    SELECT CASE 
        WHEN @Array REGEXP CONCAT('((,).*){',@ArrayIndex,'}') 
        THEN SUBSTRING_INDEX(SUBSTRING_INDEX(@Array,',',@ArrayIndex+1),',',-1) 
        ELSE NULL
    END AS Result;
    
    • SUBSTRING_INDEX(string, delim, n) returns the first n
    • SUBSTRING_INDEX(string, delim, -1) returns the last only
    • REGEXP '((delim).*){n}' checks if there are n delimiters (i.e. you are in bounds)

提交回复
热议问题