mysql中將一行轉化為多行資料

2021-09-03 01:15:02 字數 812 閱讀 7489

之前在網上搜尋了很多關於這個問題的解答方式,基本都是使用substring_index和mysql.help_topic搭配使用,方法沒錯,但是使用後發現執行時間太長,因此就放棄使用網上推薦的方法。

可以使用substring_index和union all搭配使用;

圖1

圖2

將圖1變為圖2,

可以這樣編寫**:

select  id,name,substring_index(shareholder,',','1') shareholder, from test  

where (length(shareholder)-length(replace(shareholder,',','')))=1

union all

select  id,name,substring_index(shareholder,',','-1') shareholder, from test  

where (length(shareholder)-length(replace(shareholder,',','')))=1

union all

select  id,name,shareholder from test  where (length(shareholder)-length(replace(shareholder,',','')))=0

即可,執行起來也是很快。

將多行輸出轉化為一行輸出

1.採用awk awk begin file 說明 awk預設將記錄分隔符 record separator即rs 設定為 n,此行 將rs設定為eof 檔案結束 也就是把檔案視為乙個記錄,然後通過gsub函式將 n替換成空格,最後輸出。2.採用sed sed a n s n t a file 說明...

mysql 一行資料拆分多行

查詢出被逗號分隔字段需要拆分的最大數量 select max length 逗號分隔的字段 length replace 逗號分隔的字段,1 from 處理表 where 條件 建立一張臨時表用於聯合查詢,方便把處理表單行記錄分隔為多行 create temporary table incre ta...

mysql多行合併一行,一行拆分多行

資料 建表語句 drop table if exists 品牌 create table 品牌 id int 0 not null,品牌 varchar 255 character set utf8 collate utf8 general ci null default null engine i...