Converting delimited string to multiple values in mysql

后端 未结 5 1814
逝去的感伤
逝去的感伤 2020-12-11 18:40

I have a mysql legacy table which contains an client identifier and a list of items, the latter as a comma-delimited string. E.g. \"xyz001\", \"foo,bar,baz\". T

5条回答
  •  离开以前
    2020-12-11 19:08

    Interesting.

    I can't think of a clean and tidy solution, but I could offer some ideas.

    I guess you could combine UNION with the following:

    http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/

    If you know the maximum number of items in any row, then you could do a union on the results .. something like:

    (SELECT col1, SPLIT_STR(col2, ',', 1) c2 from tbl)
    UNION (SELECT col1, SPLIT_STR(col2, ',', 2) c2 from tbl where col2 like '%,%')
    UNION (SELECT col1, SPLIT_STR(col2, ',', 3) c2 from tbl where col2 like '%,%,%')
    ...
    

    I'm not very up on mysql stored procedures, but perhaps you could do some clever stuff with loops in order to make this more dynamic.

    I hope this points you in the right direction - ?

提交回复
热议问题