将列字符串拆分为多个列字符串

发布于 2024-11-26 03:29:20 字数 1258 浏览 0 评论 0原文

我在表中有一个条目,它是一个由分号分隔的字符串。是否可以将字符串拆分为单独的列?我一直在网上和 stackoverflow 上查找,但找不到可以拆分成列的工具。

表中的条目看起来像这样(括号 [] 中的任何内容实际上都不在我的表中。只是为了让事情更清楚):

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 中,查询是否可以

  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
...

为表中的所有行返回以下内容,其中逗号表示新列?

编辑:

这是 Bohemian 请求的一些输入和输出。

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>>

有些条目可能较长,但它们都具有相似的格式。这是我希望的输出:

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

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]
...

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?

Edit:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风蛊 2024-12-03 03:29:20

您可以在 mysql 中使用字符串函数(特别是 substr):http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

You could make use of String functions (particularly substr) in mysql: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

半夏半凉 2024-12-03 03:29:20

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

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 - 基于 SELECT 查询的 UPDATE 查询STR_SPLIT 问题将值从一个字段拆分为两个

是的,我只是拆分为2、SUBSTRING 可能不太适合你,但无论如何,希望这会有所帮助:)

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)

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文