mysql逗號分隔List欄位轉多行

2021-10-07 17:45:40 字數 1994 閱讀 5380

具體的邏輯我還沒整明白,先記上再說,親測可用。

select a1.id,a1.job_depends

from job_version_history a1

where a1.id in (1655,1656);

id    job_depends

1655    353,3,532

1656    484,5,567

select a1.id,a1.job_depends,substring_index(substring_index(a1.job_depends,',',a2.id+0),',',-1) job_depends_id

from job_version_history a1

join job a2 on a2.id < (length(a1.job_depends) - length(replace(a1.job_depends,',',''))+2)

where a1.id in (1655,1656);

id    job_depends    job_depends_id

1655    353,3,532    353

1655    353,3,532    3

1655    353,3,532    532

1656    484,5,567    484

1656    484,5,567    5

1656    484,5,567    567

說明:這個job裡的id,是乙個自增的列;它在這裡的作用就是提供乙個足夠大的序列。

select a11.job_id,

a11.job_name,

a11.job_owner_name,

a11.last_release_time,

a11.project_name,

a11.job_type,

group_concat(a11.job_depends_split_id) job_depends_id,

group_concat(a22.name) job_depends_name

from

(select a1.job_id,

a1.job_name,

a1.job_owner_name,

a1.job_release_time last_release_time,

a2.`name` project_name,

a1.job_type,

a1.job_depends,

substring_index(substring_index(a1.job_depends,',',a9.id+0),',',-1) job_depends_split_id

from job_version_history a1

left join project a2 on a1.project_id=a2.id

join job a9 on a9.id < (length(a1.job_depends) - length(replace(a1.job_depends,',',''))+2)

where a1.job_end_version = 2147483647) a11

left join job a22 on a11.job_depends_split_id=a22.id

group by a11.job_id,

a11.job_name,

a11.job_owner_name,

a11.last_release_time,

a11.project_name,

a11.job_type;

說明:合成以逗號分隔的多行轉一行關鍵字group_concat。

mysql 逗號分隔查詢

商品表 goods 有學校字段 school ids 學校字段 school ids 中儲存學校表 school 的id,如果商品包含多個學校則多個id使用逗號分隔儲存 查詢sql select g.id,g.school ids,group concat s.locality name from ...

mysql 當字段以逗號分隔時的轉換

當乙個欄位裡的資料是以逗號分隔時,利用group concat str 函式以及substring index 函式,將其資料直接轉換成以逗號相隔的字串 記錄sql如下 select s.id,group concat d.dict name dict name from sys data dict...

mysql 逗號分隔的id轉為逗號分隔的名稱

mysql 的一張表a的某1欄位是用逗號分隔的code,每個code對應在b表的有code和名稱的對應表。如果用語句檢索出,用逗號分隔的名稱拼接在一起。select b.engine number,group concat code name astran name fromt fix code a...