Mysql comma delimited column conversion summary

Source: Internet
Author: User

方法一
Stored Procedure implementations:
--Modify Terminator to prevent the default semicolon from running directly on the MySQL command line
Delimiter $$
--Create a function to calculate the number of strings after splitting
Drop function if exists calc_length $$
Create function calc_length (str varchar (), SPLITSTR varchar (5)) returns INT (11)
Begin
return Length (str)-length (replace (str, SPLITSTR, ')) +1;
End $$
--Create a function to simulate split split string
Drop function if exists split_string $$
Create definer= ' root ' @ ' localhost ' function split_string (str varchar), SPLITSTR varchar (5), Strindex int) returns varchar (255)
Begin
DECLARE result varchar (255) default ';
Set result =reverse (Substring_index (Reverse (Substring_index (str, SPLITSTR, strindex)), SPLITSTR, 1);
return result;
End $$
--Create a stored procedure
drop procedure if exists proce_split $$
CREATE PROCEDURE Proce_split ()
Begin
DECLARE CNT int default 0;
declare i int default 0;
DECLARE str varchar (+) default ';
Select name into str from table1 where id = 2;
Set cnt = calc_length (str, ', ');
drop table if exists table2;
Create table2 (id int not NULL, name varchar (255) is not NULL) default Charset=utf8;
While I < CNT
Do
Set i = i+1;
INSERT into table (ID, name) values (i, split_string (str, ', ', i));
End while;
End $$
Method Two:
Cartesian Product Realization:
Select A.id,substring_index (Substring_index (A.loginname, ', ', b.help_topic_id+1), ', ',-1)
From
Account A
Join
Mysql.help_topic b
On b.help_topic_id < (length (a.loginname)-Length (replace (A.loginname, ', ', '))) +1)
Order BY a.ID;

Mysql comma delimited column conversion summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.