MySQL:将逗号分隔的列表拆分为多行
我有一个非规范化的表,其中有一列包含逗号分隔的列表,该列表是另一个表的外键:
+----------+-------------+ +--------------+-------+
| part_id | material | | material_id | name |
+----------+-------------+ +--------------+-------+
| 339 | 1.2mm;1.6mm | | 1 | 1.2mm |
| 970 | 1.6mm | | 2 | 1.6mm |
+----------+-------------+ +--------------+-------+
我想将此数据读入不提供过程语言的搜索引擎。
那么有没有办法对此列进行联接或对此数据运行查询,将适当的条目插入到新表中? 结果数据应该如下所示:
+---------+-------------+
| part_id | material_id |
+---------+-------------+
| 339 | 1 |
| 339 | 2 |
| 970 | 2 |
+---------+-------------+
如果 DBMS 支持返回表的函数,但 MySQL 显然不支持,我可以想到一个解决方案。
I have an unnormalized table with a column containing a comma separated list that is a foreign key to another table:
+----------+-------------+ +--------------+-------+
| part_id | material | | material_id | name |
+----------+-------------+ +--------------+-------+
| 339 | 1.2mm;1.6mm | | 1 | 1.2mm |
| 970 | 1.6mm | | 2 | 1.6mm |
+----------+-------------+ +--------------+-------+
I want to read this data into a search engine that offers no procedural language.
So is there a way to either make a join on this column or run a query on this data that inserts appropriate entries into a new table?
The resulting data should look like this:
+---------+-------------+
| part_id | material_id |
+---------+-------------+
| 339 | 1 |
| 339 | 2 |
| 970 | 2 |
+---------+-------------+
I could think of a solution if the DBMS supported functions returning a table but MySQL apparently doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在很多天里回答了两个类似的问题,但没有得到任何答复,所以我猜人们会因为使用光标而被推迟,但因为它应该是一个一次性的过程,我个人认为这并不重要。
正如您所说,MySQL 还不支持表返回类型,因此除了循环表并解析材料 csv 字符串并为零件和材料生成适当的行之外,您别无选择。
以下帖子可能会引起兴趣:
split keywords for post php mysql
将数据从临时表加载到其他表的 MySQL 过程。流程中需要拆分多值字段
Rgds
I've answered two similar questions in as many days but not had any responses so I guess people are put off by the use of the cursor but as it should be a one off process I personally dont think that matters.
As you stated MySQL doesnt support table return types yet so you have little option other than to loop the table and parse the material csv string and generate the appropriate rows for part and material.
The following posts may prove of interest:
split keywords for post php mysql
MySQL procedure to load data from staging table to other tables. Need to split up multivalue field in the process
Rgds
MySQL 没有临时表重用,并且函数不返回行。
我在 Stack Overflow 中找不到任何可以将 csv 整数字符串转换为行的内容,所以我在 MySQL 中编写了自己的字符串。
如果不使用整数,您还需要转换为不同的类型。
MySQL does not have temporary table reuse and functions do not return rows.
I can't find anything in Stack Overflow to convert string of csv integers into rows so I wrote my own in MySQL.
You will also need to cast to different types if you are not using ints.
您还可以使用 REGEXP
如果您只想获取一部分,这将有所帮助。当然不是整张桌子
You can also use REGEXP
This will help if you want to get just one part. Not the whole table of course
在 MySQL 中,可以如下实现
现在要获取逗号分隔的vehicle_ids 的长度,请使用下面的查询
有关更多信息,请访问 http://amitbrothers.blogspot.in/2014/03/mysql-split-comma-separated-list-into.html
In MySQL this can be achieved as below
Now to get the length of comma separated vehicle_ids use below query
For more info visit http://amitbrothers.blogspot.in/2014/03/mysql-split-comma-separated-list-into.html