MySQL CSV 行到多行
我需要将旧数据库迁移到新数据库。不幸的是,编写旧数据库的人使用带有逗号分隔外键的字段创建了 n,n 关系。
我想编写一个 mysql 查询(也许使用 insert into ... select)来分割这些逗号分隔的外键,以便我可以构建一个表,其中每行都是外键。
这可能吗?
I need to migrate an old database to my new one. Unfortunately the guy who wrote the old database created an n,n relation using a field with comma separated foreign keys.
I would like to write a mysql query (maybe using insert into ... select) that splits those comma seperated foreign keys so that i can build a table where each row is a foreign key.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在纯 SQL 中执行此操作并不简单。最简单的方法是使用您选择的编程语言依次检索每条记录,并根据逗号分隔字段插入多对多连接表记录。以下伪代码建议您可以使用的方法:
完成此操作后,可以删除包含逗号分隔外键的现有列。
It's not straightforward to do this in pure SQL. It will be easiest to retrieve each record in turn using a programming language of your choice and insert the many-to-many join table records based on the comma separated field. The following pseudo code suggests an approach that you might use:
Once you've done this, the existing column holding the comma separated foreign keys can be removed.
我的解决方案
My solution