在MySQL中获取多分隔字符串并转换为多条记录

发布于 2024-10-08 16:31:11 字数 580 浏览 8 评论 0原文

我正在将旧数据集转换为新结构化的数据库。目前,他们拥有以下格式的数据:

[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

我想在每个记录中获取该字符串并执行以下操作:

  1. 根据每个分号分割在现有表中创建一条新记录通过
  2. 波浪号再次分割每个标记,将第一个部分放入一个字段中,将第二个部分放入一个字段中在同一个记录中的单独字段(我不关心通过管道分割)

我不知道它是否可能,但我希望它是。我也希望只需要执行一次,因为新系统将自动创建所有这些垃圾。我的 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:

  1. Create a new record in an existing table based off each semicolon split
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-10-15 16:31:11

这在 SQL 中似乎是一件相当恶心的事情,但这里有一个概念验证脚本。

如果输出看起来正确,请将 SELECT 替换为适当的 INSERT 语句,它应该会得到您想要的结果。

delimiter ;;

drop procedure if exists load_crazy_stuff;;

create procedure load_crazy_stuff(in s longtext)
begin
   declare pos       int;
   declare record    longtext;
   declare leftpart  int;
   declare rightpart longtext;
   set s = concat(s,';');
   while length(s)>0 DO
     set pos       = instr(s,';');
     set record    = left(s,pos-1);
     set s         = substr(s,pos+1);
     set pos       = instr(record,'~');
     set leftpart  = left(record,pos-1);
     set rightpart = substr(record,pos+1);
     select leftpart, rightpart;
   end while;
end;;

call load_crazy_stuff('3~S|Red|Top;1~S|Blue|Top;20~XL|Green|Left');;

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.

delimiter ;;

drop procedure if exists load_crazy_stuff;;

create procedure load_crazy_stuff(in s longtext)
begin
   declare pos       int;
   declare record    longtext;
   declare leftpart  int;
   declare rightpart longtext;
   set s = concat(s,';');
   while length(s)>0 DO
     set pos       = instr(s,';');
     set record    = left(s,pos-1);
     set s         = substr(s,pos+1);
     set pos       = instr(record,'~');
     set leftpart  = left(record,pos-1);
     set rightpart = substr(record,pos+1);
     select leftpart, rightpart;
   end while;
end;;

call load_crazy_stuff('3~S|Red|Top;1~S|Blue|Top;20~XL|Green|Left');;
GRAY°灰色天空 2024-10-15 16:31:11

非粗略的方法是这样的:

load data infile '/tmp/your-data-file' into table yourtable fields terminated by '~' lines terminated by ';';

The non-gross way to do it is this:

load data infile '/tmp/your-data-file' into table yourtable fields terminated by '~' lines terminated by ';';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文