mysql按字段拆分表_MYSQL命令怎么实现将表中某个字段中多个记录拆分,急求答案!...

展开全部

|

觉得你还62616964757a686964616fe78988e69d8331333332623262是应该用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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值