在MySQL中获取多分隔字符串并转换为多条记录
我正在将旧数据集转换为新结构化的数据库。目前,他们拥有以下格式的数据:
[quantity int]~[var1 string]|[var2 string optional]|[var3 string optional];(etc);
[quantity]~[var1]|[var2]|[var3]
其中各种模式首先由分号分隔,然后由波形符分隔,最后由现有数据库中同一字段(无括号)内的竖线字符分隔。例如:
3~S|Red|Top;1~S|Blue|Top; ... etc ... ;20~XL|Green|Left
我想在每个记录中获取该字符串并执行以下操作:
- 根据每个分号分割在现有表中创建一条新记录通过
- 波浪号再次分割每个标记,将第一个部分放入一个字段中,将第二个部分放入一个字段中在同一个记录中的单独字段(我不关心通过管道分割)
我不知道它是否可能,但我希望它是。我也希望只需要执行一次,因为新系统将自动创建所有这些垃圾。我的 sproc-fu 在 MySQL 中非常糟糕,所以非常感谢任何帮助。
非常感谢!
I am converting an old dataset into a newly structured database. Currently they have data in this format:
[quantity int]~[var1 string]|[var2 string optional]|[var3 string optional];(etc);
[quantity]~[var1]|[var2]|[var3]
where various patterns are delimited first by semicolons, then by tildes, and finally by pipe characters within the same field (no brackets) within the existing database. For instance:
3~S|Red|Top;1~S|Blue|Top; ... etc ... ;20~XL|Green|Left
I want to take that string in each record and do the following:
- Create a new record in an existing table based off each semicolon split
- Split each token again by the tilde, putting the first piece into one field and the second into a separate field (I don't care about splitting by pipe) in the same record
I don't know if it's even possible, but I'm hoping that it is. I am also hoping to only have to do this once, as the new system will take care of creating all this crap automatically. My sproc-fu is quite awful in MySQL, so any help is much appreciated.
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在 SQL 中似乎是一件相当恶心的事情,但这里有一个概念验证脚本。
如果输出看起来正确,请将 SELECT 替换为适当的 INSERT 语句,它应该会得到您想要的结果。
This seems like a pretty gross thing to do in SQL, but here's a proof-of-concept script.
If the output looks right to you, replace the SELECT with an appropriate INSERT statement and it should get you what you want.
非粗略的方法是这样的:
The non-gross way to do it is this: