简体   繁体   English

将逗号分隔的数据拆分为多行

[英]Split separated data by comma into multiple rows

I want to modify table in my production server by extracting some column and put it into another table, the problem is the old column has multiple value separated by commas and in new table will be put in multiple records by query.我想通过提取一些列并将其放入另一个表来修改我的生产服务器中的表,问题是旧列有多个以逗号分隔的值,而在新表中将通过查询放入多条记录。 i know the way like INSERT INTO user_emails(user_id, email) SELECT id, email FROM users but it would copy entire content of field.我知道像INSERT INTO user_emails(user_id, email) SELECT id, email FROM users但它会复制字段的整个内容。

table users (old)
-----------------------------------------------------
id  name  emails (removed)
-----------------------------------------------------
1   A     aaa@email.com, bbb@email.com
2   B     ccc@email.com, ddd@email.com, eee@email.com

table user_emails (new table)
---------------------------------
id  user_id  email
---------------------------------
1   1        aaa@email.com
2   1        bbb@email.com
3   2        ccc@email.com
4   2        ddd@email.com
5   2        eee@email.com

Any solution?有什么解决办法吗? Thank you谢谢

you should get fetch data from DB and store into array and then use explode() function to split the data您应该从数据库中获取数据并存储到数组中,然后使用explode()函数来拆分数据

<?php
   $data=explode("," , $allEmails);
?>

now $data is convert into array like this现在$data被转换成这样的数组

Array ( [0] => abc@g.com [1] => a@yahoo.com [2] => z@y.com )

finally you can insert into new table using insert query最后,您可以使用插入查询插入新表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM