早教吧作业答案频道 -->其他-->
mysql一个字段拆分多个字段mysql如何将表中的一个字段拆分成多个字段中间用','隔开如表idnamename1name2...1张三,李四2,王二,李王,小明拆分成idnamename
题目详情
mysql 一个字段拆分多个字段
mysql如何将表中的一个字段拆分成多个字段中间用','隔开如
表
id name name1 name2 ...
1 张三,李四
2, 王二,李王,小明
拆分成
id name name1 name2 name3 ...
1 张三 , 李四
2, 王二,李王,小明 王二 李王 小明 ...
求哪位大神
错了
拆分成
id name name1 name2 name3 ...
1 张三,李四 张三 李四
2, 王二,李王,小明 王二 李王 小明 ...
mysql如何将表中的一个字段拆分成多个字段中间用','隔开如
表
id name name1 name2 ...
1 张三,李四
2, 王二,李王,小明
拆分成
id name name1 name2 name3 ...
1 张三 , 李四
2, 王二,李王,小明 王二 李王 小明 ...
求哪位大神
错了
拆分成
id name name1 name2 name3 ...
1 张三,李四 张三 李四
2, 王二,李王,小明 王二 李王 小明 ...
▼优质解答
答案和解析
觉得你还是应该用php去写。非要用mysql本身的话给个参考:
mysql> select * from a;+------+--------+------+----+| id | name | num1 | bb |+------+--------+------+----+| 1 | a, | 2 | 3 || 2 | b,c, | 2 | 4 || 3 | d,e,f, | 2 | 5 |+------+--------+------+----+3 rows in set (0.00 sec)mysql> delimiter //mysql> create procedure `strsplit1`(in str3 varchar(2)) -> begin -> declare i int(10); -> declare j int(10); -> declare k int(10); -> declare str1 varchar(100); -> declare str4 varchar(1000); -> declare str5 varchar(100); -> -> select count(*) into i from a; -> if i!=0 then -> drop table if exists tmp_table1; -> CREATE TEMPORARY TABLE tmp_table1 like a; -> end if; -> -> set j=0; -> while j select id,name into str1,str4 from a limit j,1; -> select instr(str4,str3) into k from dual; -> if k=0 then -> insert into tmp_table1(id,name) values(str1,str4); -> end if; -> while k!=0 do -> select substring_index(str4,str3,1) into str5 from dual; -> insert into tmp_table1(id,name) values(str1,str5); -> select mid(str4,k+1) into str4 from dual; -> select instr(str4,str3) into k from dual; -> end while; -> set j=j+1; -> end while; -> -> select * from tmp_table1; -> end -> //Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;最后结果:mysql> call strsplit1(',');+------+------+------+----+| id | name | num1 | bb |+------+------+------+----+| 1 | a | NULL | 1 || 2 | b | NULL | 2 || 2 | c | NULL | 3 || 3 | d | NULL | 4 || 3 | e | NULL | 5 || 3 | f | NULL | 6 |+------+------+------+----+6 rows in set (0.25 sec)Query OK, 0 rows affected (0.30 sec)
希望能解决您的问题。
mysql> select * from a;+------+--------+------+----+| id | name | num1 | bb |+------+--------+------+----+| 1 | a, | 2 | 3 || 2 | b,c, | 2 | 4 || 3 | d,e,f, | 2 | 5 |+------+--------+------+----+3 rows in set (0.00 sec)mysql> delimiter //mysql> create procedure `strsplit1`(in str3 varchar(2)) -> begin -> declare i int(10); -> declare j int(10); -> declare k int(10); -> declare str1 varchar(100); -> declare str4 varchar(1000); -> declare str5 varchar(100); -> -> select count(*) into i from a; -> if i!=0 then -> drop table if exists tmp_table1; -> CREATE TEMPORARY TABLE tmp_table1 like a; -> end if; -> -> set j=0; -> while j select id,name into str1,str4 from a limit j,1; -> select instr(str4,str3) into k from dual; -> if k=0 then -> insert into tmp_table1(id,name) values(str1,str4); -> end if; -> while k!=0 do -> select substring_index(str4,str3,1) into str5 from dual; -> insert into tmp_table1(id,name) values(str1,str5); -> select mid(str4,k+1) into str4 from dual; -> select instr(str4,str3) into k from dual; -> end while; -> set j=j+1; -> end while; -> -> select * from tmp_table1; -> end -> //Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;最后结果:mysql> call strsplit1(',');+------+------+------+----+| id | name | num1 | bb |+------+------+------+----+| 1 | a | NULL | 1 || 2 | b | NULL | 2 || 2 | c | NULL | 3 || 3 | d | NULL | 4 || 3 | e | NULL | 5 || 3 | f | NULL | 6 |+------+------+------+----+6 rows in set (0.25 sec)Query OK, 0 rows affected (0.30 sec)
希望能解决您的问题。
看了 mysql一个字段拆分多个字...的网友还看了以下:
有8张卡片,其中5张分别写有A、B、C、D、E五个大写字母,另外三张分别写有x、y、z三个小写字母 2020-04-26 …
lim(x趋于0时)[(e^x+e^2x+e^3x+……e^nx)/n]^(1/x)的极限;我自己 2020-06-12 …
请问后张法预制T梁张拉中孔道偏差系数和孔道摩阻变大,钢绞线伸长值会变大吗?能否用Pp=P[1-e^ 2020-06-25 …
不定积分e^3x/(e^4x-1)还有不定积分e^x/根号(1+e^2x)请叙述清楚些好吗?还有这 2020-07-04 …
利用求导判断公切线?做了一半如何做下去?函数y=ax^2与y=lnx的图形的公切线方程为y=(1/ 2020-08-01 …
如何判断公切线函数y=ax^2与y=lnx的图形的公切线方程为y=(1/根号下e)*x-1/2判断 2020-08-01 …
用C++求不超过30000E数列的最大E数的值/*数列:E(1)=E(2)=1E(n)=(n-1)* 2020-11-20 …
抽奖先后,概率是否一样有6张纸条,上面分别写着1~6六个数字,小明从中任意选出一张放在手心,让A~E 2020-12-02 …
函数的连续及判断(e^x分之1)-e^-x分之1)/(e^x分之1+e-x分之1)=(e^x分之2) 2020-12-17 …
2010上海世博会分五个展区,其中A、B、C三个片区位于浦东,D、E两个片区位于浦西,小明、小丽都是 2020-12-24 …