MySQL splits fields through delimiters, that is, converting one row to multiple rows

1. The original data of the table is as follows:

SELECT id,warehouse_ids FROM `test` ORDER BY warehouse_ids DESC LIMIT 1

2. Split queries based on commas

Notice

1. The delimiter needs to be replaced with the corresponding character. Here I am comma (,)

2. The user table of JOIN is a table in my database. Its main purpose is to use its auto-increment id. The auto-increment id must start from 1 continuously (1, 2, 3....)

3. Generally, the mysql.help_topic table is used instead of user. Since there is no permission for this table, user is used instead.

SELECT
	t.id AS 'id',
	substring_index( substring_index( t.warehouse_ids, ',', h.id  ), ',',- 1 ) AS 'warehouse_id' 
FROM
	(SELECT id,warehouse_ids FROM `test` ORDER BY warehouse_ids DESC LIMIT 1) t
	JOIN user AS h ON h.id-1 < ( char_length( t.warehouse_ids ) - char_length( REPLACE ( t.warehouse_ids, ',', '' ) ) + 1 )

Referenced the following sql

SELECT
    t.id AS 'id',
    t.NAME AS '姓名',
    substring_index( substring_index( t.courses, '、', h.help_topic_id + 1 ), '、',- 1 ) AS '课程'
FROM t_student t JOIN mysql.help_topic AS h ON h.help_topic_id < ( char_length( t.courses ) - char_length( REPLACE ( t.courses, '、', '' ) ) + 1 )
;

3. Reference documents

MySQL splits fields through delimiters, that is, converting one row to multiple rows in SQL writing

mysql splits the string into multiple lines (separated by commas, etc.)

Guess you like

Origin blog.csdn.net/weixin_42048982/article/details/130870118