MySQL 数组数据类型,拆分字符串,

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4078838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:36:52  来源:igfitidea点击:

Array data type, split string,

mysql

提问by gumpi

I'm currently working on a function in MYSQL, I have a comma delimited string (1,22,344,55) from another table, How can I split this in MYSQL to an array (NOT temp_table). Also, is there a similar function in MYSQL where I can do foreach()?

我目前正在 MYSQL 中处理一个函数,我有一个来自另一个表的逗号分隔字符串 (1,22,344,55),我如何将它在 MYSQL 中拆分为一个数组(不是 temp_table)。另外,在 MYSQL 中是否有类似的功能,我可以在其中执行 foreach()?

回答by Roman Goyenko

MySQL does not include a function to split a delimited string. However, it's very easy to create your own function.

MySQL 不包含拆分分隔字符串的函数。但是,创建自己的函数非常容易。

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

Usage

用法

SELECT SPLIT_STR(string, delimiter, position)

From here: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/

从这里:http: //blog.fedecarg.com/2009/02/22/mysql-split-string-function/

回答by Luxo Mansilla

The correct usage in MySQL is:

MySQL中的正确用法是:

SELECT FIND_IN_SET('b','a,b,c,d');

Gives as result: 2

结果给出:2

回答by Parris Varney

SQL's version of foreach is called a cursor

SQL 的 foreach 版本称为游标