将列字符串拆分为多个列字符串 [英] Split column string into multiple columns strings

查看:126
本文介绍了将列字符串拆分为多个列字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有一个条目,它是一个用分号分隔的字符串.可以将字符串分成单独的列吗?我一直在寻找联机状态和stackoverflow,但找不到一个可以将其拆分为多个列的方法.

I have a entry in the table that is a string which is delimited by semicolons. Is possible to split the string into separate columns? I've been looking online and at stackoverflow and I couldn't find one that would do the splitting into columns.

表中的条目看起来像这样(方括号[]中的任何内容实际上都不在我的表中.只是在这里使内容更清楚):

The entry in the table looks something like this (anything in brackets [] is not actually in my table. Just there to make things clearer):

sysinfo [column]
miscInfo ; vendor: aaa ; bootr: bbb; revision: ccc; model: ddd [string a]
miscInfo ; vendor: aaa ; bootr: bbb; revision: ccc; model: ddd [string b]
...

大约有一百万个带有这样的字符串的条目.在mySQL中是否可能使查询返回以下内容

There are a little over one million entries with the string that looks like this. Is it possible in mySQL so that the query returns the following

  miscInfo,   Vendor,   Bootr,  Revision ,  Model [columns]
miscInfo_a, vendor_a,  bootr_a, revision_a, model_a
miscInfo_b, vendor_b,  bootr_b, revision_b, model_b
...

对于表中的所有行,其中的逗号表示新的列?

for all of the rows in the table, where the comma indicates a new column?

这里是波希米亚人要求的一些输入和输出.

Here's some input and output as Bohemian requested.

sysinfo [column]
Modem <<HW_REV: 04; VENDOR: Arris ; BOOTR: 6.xx; SW_REV: 5.2.xxC; MODEL: TM602G>>

<<HW_REV: 1; VENDOR: Motorola ; BOOTR: 216; SW_REV: 2.4.1.5; MODEL: SB5101>>

Thomson DOCSIS Cable Modem <<HW_REV: 4.0; VENDOR: Thomson; BOOTR: 2.1.6d; SW_REV: ST52.01.02; MODEL: DCM425>>

某些条目可以更长,但它们的格式都相似.这就是我想要的输出:

Some can be longer entries but they all have similar format. Here is what I would like the output to be:

miscInfo, vendor, bootr, revision, model [columns]
    04,   Arris,  6.xx, 5.2.xxC, TM602G
    1, Motorola, 216, 2.4.1.5, SB5101
  4.0, Thomson, 2.1.6d, ST52.01.02, DCM425

推荐答案

请查看我如何将坐标列分为2个经度/纬度列:

Please take a look at how I've split my coordinates column into 2 lat/lng columns:

UPDATE shops_locations L 
LEFT JOIN shops_locations L2 ON L2.id = L.id
SET L.coord_lat = SUBSTRING(L2.coordinates, 1, LOCATE('|', L2.coordinates) - 1), 
L.coord_lng = SUBSTRING(L2.coordinates, LOCATE('|', L2.coordinates) + 1)

总体而言,我从这里遵循了UPDATE JOIN的建议 MySQL-基于UPDATE查询SELECT查询STR_SPLIT问题,请在此处从一场到两场

In overall I followed UPDATE JOIN advice from here MySQL - UPDATE query based on SELECT Query and STR_SPLIT question here Split value from one field to two

是的,我只是分成2个部分,SUBSTRING可能对您来说效果不佳,但是无论如何,希望这会有所帮助:)

Yes I'm just splitting into 2, and SUBSTRING might not work well for you, but anyway, hope this helps :)

这篇关于将列字符串拆分为多个列字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆