MySQL 中是否有按索引的 FIND_IN_SET? [英] Is there a FIND_IN_SET by index in MySQL?

查看:58
本文介绍了MySQL 中是否有按索引的 FIND_IN_SET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道将列表存储为字符串并不明智,但我必须处理这个问题以导出一些以这种方式存储的数据.我也知道 FIND_IN_SET 函数,它返回列表中某个字符串的索引:

I know storing lists as strings is not wise, but I have to deal with that to export some data stored that way. I also know the FIND_IN_SET function, which returns the index of a certain string in a list:

SELECT FIND_IN_SET('bar', 'foo,bar,baz');
-- selects 2

是否有任何内置函数(或函数组合)可以在列表的特定索引中获取字符串?我正在寻找这样的东西:

Is there any built-in function (or combination of functions) to get the string in a particular index of the list? I'm looking for something like this:

SELECT IN_SET_AT_INDEX(2, 'foo,bar,baz');
-- selects 'bar'

如果可能,我想避免使用类似拆分的函数使列表成为一个单独的表格.

I'd like to avoid a split-like function that makes the list a separate table, if possible.

推荐答案

SUBSTRING_INDEX() 可以做到这一点,有点:

SUBSTRING_INDEX() can do this, sort of:

mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('foo,bar,baz', ',', 2), ',', -1) AS middle_one;
+------------+
| middle_one |
+------------+
| bar        |
+------------+

这篇关于MySQL 中是否有按索引的 FIND_IN_SET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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