Array data type, split string,

2020-01-29 11:56发布

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
3条回答
在下西门庆
2楼-- · 2020-01-29 12:29

SQL's version of foreach is called a cursor

查看更多
看我几分像从前
3楼-- · 2020-01-29 12:38

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

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/

查看更多
够拽才男人
4楼-- · 2020-01-29 12:49

The correct usage in MySQL is:

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

Gives as result: 2

查看更多
登录 后发表回答