MySQL 查詢語句,將用逗號拼接的字段,拆分成多條數據進行顯示

        SELECT DISTINCT
			info.`id`,
			SUBSTRING_INDEX(
				SUBSTRING_INDEX(
					info.`material_id`,
					',',
					b.help_topic_id + 1
				),
				',',
				- 1
			) AS material_id
		FROM
			(
				A
			) info
		JOIN mysql.help_topic b ON b.help_topic_id < (
			LENGTH(info.`material_id`) - LENGTH(
				REPLACE (info.`material_id`, ',', '')
			) + 1
		)
		WHERE
			1 = 1 
            -- info.`id` = '1243360010160885760'

結果集A是:

            SELECT
                    a.id,
                    a.material_id
                FROM
                    t_goodinfo a
                LEFT JOIN t_material b ON a.material_id = b.id
                AND b. STATUS = 0
                WHERE
                    a. STATUS = 0
                AND LOCATE(",", a.material_id)

上面的結果集,是物料id用逗號拼接的數據

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章