MYSQL 動態查詢結果

2021-09-24 20:12:07 字數 4365 閱讀 8091

今天遇到乙個需求需要把這樣的

轉化為這樣的

第一張圖是從臨時表來的,也就是第二個圖的列名是不固定的,如果列名是固定的就是乙個簡單的行轉列,但是現在列名不固定,我們必須使用動態的行轉列, 完成這個裝換我們需要使用兩個知識點

1. 使用動態語句執行sql

set @sql = concat("create temporary table dy_table( ser_no varchar(40) null,", @partial_sql, ");");

prepare stmt from @sql;

execute stmt;

2. 實現行轉列

select order_no, order_type,

max(case when profile_type ='assy_loc' and profile_cat='mps' then profile_c else null end) as 'assyloc',

max(case when profile_type ='shift' and profile_cat='mps' then profile_c else null end) as 'shif',

max(case when profile_type ='priority' and profile_cat='mps' then profile_c else null end) as 'priority',

max(case when profile_type ='rel_date' and profile_cat='mps' then profile_d else null end) as 'releasedate',

from test

where test_type = 28

and test_no in(10504530, 10530423)

group by test_no , test_type

因為需求中有一部分列是固定的,有一部分列是不固定的,我們需要把不固定的列插入乙個臨時表中,最後和固定的列以及join查出來。大概思路:

1. 將value_s列進行拆分,使用字串把欄位名,和值拆分到乙個臨時表中

2. 根據欄位名去重,構建臨時表的建立sql, 和插入sql

3. 執行構建的sql.

但是這裡有乙個小問題,就是使用的多行轉一行的函式group_concat是有長度限制的,預設是1024, 超過這個長度就會被截斷,你拼接的sql就是不全面的. 查詢長度使用:show variables like "group_concat_max_len";

有三種解決方案, 選擇其中一種即可:

1. set session group_concat_max_len = 1000000; (session級別的)

2. set global group_concat_max_len=102400

3. 在配置檔案中

在mysql配置檔案中my.conf或my.ini中新增:

#[mysqld]

group_concat_max_len=102400

然後 重啟mysql服務

完整**

drop temporary table

if exists assy_detail;

drop temporary table

if exists dy_table;

drop temporary table

if exists create_dy_table;

drop temporary table

if exists dy_column;

create temporary table assy_detail(

ser_no varchar(100) null,

value_s varchar(100) null

);create temporary table create_dy_table(

cloumn_name varchar(100) null,

create_table_sql varchar(100) null,

insert_data_sql varchar(100) null

);create temporary table dy_column(

ser_no varchar(40) null

,value_s varchar(256) null

,dy_query_sql varchar(100) null

,cloumn_name varchar(50) null

,cloumn_value varchar(30) null

);insert into assy_detail(ser_no,value_s) values ('123', '姓名:張三');

insert into assy_detail(ser_no,value_s) values ('123', '性別:男');

insert into assy_detail(ser_no,value_s) values ('123', '學歷:本科');

insert into assy_detail(ser_no,value_s) values ('1233', '學歷:本科');

select * from assy_detail;

/*1. create temporary table dy_table */

insert into create_dy_table(

cloumn_name

)select distinct replace(substring(value_s, 1, locate(":", value_s) - 1), '-', '_')

from assy_detail;

update create_dy_table

set create_table_sql = concat(cloumn_name, ' varchar(100) null '),

insert_data_sql = concat(' max(case when cloumn_name = ', '\'', cloumn_name, '\'', ' then ', 'cloumn_value ', ' else null end) as ', '\'', cloumn_name, '\'');

set @partial_sql = (

select group_concat(create_table_sql)

from create_dy_table

);set @sql = concat("create temporary table dy_table( ser_no varchar(40) null,", @partial_sql, ");");

prepare stmt from @sql;

execute stmt;

/*2 insert data into dy_table*/

insert into dy_column(

ser_no

,value_s

,cloumn_name

,cloumn_value

)select distinct

ser_no

,value_s

,replace(substring(value_s, 1, locate(":", value_s) - 1), '-', '_')

,substring(value_s, locate(":", value_s) + 1)

from assy_detail;

set @partial_sql = (

select group_concat(insert_data_sql)

from create_dy_table

);set @sql = concat('insert into dy_table select ser_no, ',@partial_sql, ' from dy_column group by ser_no;');

prepare stmt from @sql;

execute stmt;

select * from dy_table;

pymysql動態查詢結果

如果程式需要檢查資料庫中的某個動態的資料,只有當這個資料滿足條件時才會繼續執行 比如需要實現的功能如下 def check status status 資料庫查詢結果 初始化當前的status值 while status 條件 status 資料庫查詢結果 如果不滿足條件則持續獲取該結果 sleep...

mysql 儲存過程 動態引數 查詢執行結果

mysql function procedure 學習使用小結 1 動態sql,即動態引數 在儲存過程中,想要直接用表名變數做引數,動態執行sql,不能直接寫 1 2 3 4 5 6 7 createprocedure tablenamechar 20 begin select fromtablen...

mysql 儲存過程 動態引數 查詢執行結果

mysql function procedure 學習使用小結 1 動態sql,即動態引數 在儲存過程中,想要直接用表名變數做引數,動態執行sql,不能直接寫 create procedure tablename char 20 begin select from tablename endmysq...