Split comma separated string into rows in mysql

前端 未结 3 1244
囚心锁ツ
囚心锁ツ 2020-12-06 02:38

When I have string list like 1, 2, 3... I\'d like to use this as one column

Ids
1
2
3

Is it possible by sql query?

ex) SELECT

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 03:20

    For MySQL 8.0.4+

    SELECT *
    FROM
      JSON_TABLE(
              CONCAT('[', '1,2,3,4', ']'),
              "$[*]"
              COLUMNS(
                  ids BIGINT(20) PATH "$"
                  )
          ) AS tt
    

    Concatenate square brackets ([]) around your string to make it into a JSON array. Then use JSON_TABLE to convert it into a table. See the MySQL JSON Table Functions for more info.

提交回复
热议问题