MySql row to column stored procedure implementation

In the process of using mysql, students will encounter a row-to-column problem, which is to convert multiple pieces of data into one piece of data and display it in multiple columns.

Method 1. Implementation Use the following stored procedure, and modify the table name accordingly.

BEGIN
declare current integer;
declare strValue tinytext ;
declare sqlValue BLOB;
declare columnCount int(4);
declare countNum INTEGER;
declare pageNumber INTEGER;
declare firstNumber integer;
set current = 0;
set countNum=0;
set columnCount=0;
set pageNumber=0;
set firstNumber=0;
drop table if exists personal_contact_temp;
CREATE TABLE `personal_contact_temp` (
`id` varchar(64) NOT NULL COMMENT '主键',
`personal_id` varchar(64) ,
`relation` int(4) ,
`name` TEXT ,
`phone` TEXT ,
`phone_status` int(4) ,
`mobile` TEXT ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8 DEFAULT CHARSET=utf8 COMMENT='Customer information contact temporary table';

set columnCount=(select max(conNum) from (select count(*) as conNum from personal_contact GROUP BY personal_id) b);
set current =(select count(*) from (select count(*) as countNum from personal_contact GROUP BY personal_id) b);
while columnCount>0 do
begin
set sqlValue='';
set sqlValue= CONCAT('alter TABLE personal_contact_temp add COLUMN relation',columnCount,'A',' INT(4),',
'add COLUMN name',columnCount,'A',' VARCHAR(255),',
'add COLUMN phone',columnCount,'A',' varchar(255),',
'add COLUMN phone_status',columnCount,'A',' int(4),',
'add COLUMN mobile',columnCount,'A',' VARCHAR(255)',';');
set @ms=sqlValue;
PREPARE stmt1 FROM @ms;
EXECUTE stmt1;
END;
set columnCount=columnCount-1;
end WHILE;

while current>0 do
set pageNumber=current-1;
set strValue = (select personal_id from personal_contact group by personal_id limit pageNumber,1);
set countNum =(select count(*) from personal_contact where personal_id =strValue);
set firstNumber=countNum;
if countNum=1 then
insert into personal_contact_temp(id,personal_id,relation,name,phone,phone_status,mobile)
(select uuid(),personal_id,relation,`name`,phone,phone_status,mobile from personal_contact where personal_id =strValue);
ELSE
while countNum>0 do
set pageNumber=countNum-1;
if(firstNumber=countNum) then
insert into personal_contact_temp(id,personal_id,relation,name,phone,phone_status,mobile)
(select uuid(),personal_id,relation,`name`,phone,phone_status,mobile from personal_contact where personal_id =strValue LIMIT pageNumber,1);
ELSE
BEGIN
set sqlValue='';
set sqlValue= CONCAT('update personal_contact_temp a left join
(select relation,`name`,phone,phone_status,mobile,personal_id from personal_contact where personal_id =',"'",strValue,"'",' limit ',pageNumber,',1) b
on a.personal_id =b.personal_id
set a.relation',countNum,'A','=b.relation,
a.name',countNum,'A','=b.`name`,
a.phone',countNum,'A','=b.phone,
a.phone_status',countNum,'A','=b.phone_status,
a.mobile',countNum,'A','=b.mobile where a.personal_id =',"'",strValue,"'",';');
set @ms=sqlValue;
PREPARE stmt1 FROM @ms;
EXECUTE stmt1;
end;
end if;
set countNum =countNum-1;
end WHILE;
end if;
set current=current-1;
end WHILE;
END;

Note that when this stored procedure is running, the return column is too large, that is, if multiple rows of data are displayed in one row, the maximum value of mysql row data will be exceeded. This can be handled as appropriate.

Method 2. Mysql provides the group_concat function

select GROUP_CONCAT(`name`,'"',',',relation,'"',',',phone) from personal_contact GROUP BY personal_id;

Through this function, you can put the data you want to query into a column, then store the data in CSV format, convert the data in the latter column into data in CSV format, and then save it as CSV and import it into the database.

More solutions are welcome.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325220303&siteId=291194637
Recommended