mysql获取第一个分隔符_MySQL-获取前3个逗号分隔的值

bd96500e110b49cbb3cd949968f18be7.png

I have a query which returns a field with a set of comma separated values. I would like to get the first three of these values into separate columns in the query result.

I can get the first, using SUBSTRING_INDEX but how can I get the other two?

SELECT

'aaaaa, bbbbb, ccccc',

SUBSTRING_INDEX('aaaaa, bbbbb, ccccc', ',', 1) AS column_one

EDIT - Oops, sorry forgot to mention. The value I want to split could have more (or less) than three strings to extract.

For example, the above string could easily be 'aaaaa' or 'aaaaa, bbbbb, ccccc, ddddd, eeeee'.

In each case, I only need the first three (or however many exist).

Any advice appreciated.

Thanks.

解决方案

You could use SUBSTRING_INDEX twice, the second one with -1 parameter:

SELECT

'aaaaa, bbbbb, ccccc',

SUBSTRING_INDEX('aaaaa, bbbbb, ccccc', ',', 1) AS column_one,

SUBSTRING_INDEX(SUBSTRING_INDEX('aaaaa, bbbbb, ccccc', ',', 2), ',', -1) AS column_two,

SUBSTRING_INDEX(SUBSTRING_INDEX('aaaaa, bbbbb, ccccc', ',', 3), ',', -1) AS column_three

If the parameter is negative, everything to the right of the final delimiter (counting from the right) is returned. Eg.

SUBSTRING_INDEX('aaaaa, bbbbb, ccccc', ',', 2) will return aaaaa, bbbbb

SUBSTRING_INDEX(aaaaa, bbbbb, ',', -1) will then return bbbbb

You also might want to use ', ' as a delimiter, or TRIM the result.

Please see fiddle here.

Edit

If you want to consider strings that might have less than three values, you could use something like this:

SELECT

s,

SUBSTRING_INDEX(s, ',', 1) AS column_one,

CASE WHEN LENGTH(s)-LENGTH(Replace(s, ',', ''))>0

THEN SUBSTRING_INDEX(SUBSTRING_INDEX(s, ',', 2), ',', -1)

ELSE NULL END AS column_two,

CASE WHEN LENGTH(s)-LENGTH(Replace(s, ',', ''))>1

THEN SUBSTRING_INDEX(SUBSTRING_INDEX(s, ',', 3), ',', -1)

ELSE NULL END AS column_three

FROM

strings

Please see fiddle here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 字段中使用逗号分隔符可能会导致数据难以查询和处理。但如果必须使用逗号分隔符,可以考虑以下两种方法: 1. 使用 FIND_IN_SET() 函数 可以使用 FIND_IN_SET() 函数来查询包含某个的行,例如: ``` SELECT * FROM table WHERE FIND_IN_SET('value', column); ``` 但是,这种方法可能会导致性能问题,特别是在大型数据集中使用。 2. 建立索引 为了提高查询性能,可以为逗号分隔建立索引。这可以通过创建一个新的表来实现,该表包含两列:原始和分解后的。 例如,如果有一个名为“tags”的字段,其中包含逗号分隔的标签,可以创建一个名为“tag_list”的新表,如下所示: ``` CREATE TABLE tag_list ( tag_id INT AUTO_INCREMENT PRIMARY KEY, tag_value VARCHAR(255), orig_value VARCHAR(255) ); INSERT INTO tag_list (tag_value, orig_value) SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(tags, ',', n), ',', -1) AS tag_value, tags AS orig_value FROM table INNER JOIN numbers ON n <= (LENGTH(tags) - LENGTH(REPLACE(tags, ',', '')) + 1); CREATE INDEX idx_tag_value ON tag_list (tag_value); ``` 这个表会将“tags”字段中的每个标签分解成一个独立的行,并为“tag_value”列建立索引,以加快查询速度。 查询时,可以使用以下语句: ``` SELECT * FROM table WHERE EXISTS ( SELECT 1 FROM tag_list WHERE orig_value = table.tags AND tag_value = 'value' ); ``` 这个查询使用 EXISTS 来检查“tag_list”表中是否有包含所需标签的行。 需要注意的是,这种方法会增加存储和维护成本,并且在插入、更新和删除数据时需要更新“tag_list”表。因此,建议仅在必要时使用此方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值