数组数据类型,拆分字符串, [英] Array data type, split string,

查看:63
本文介绍了数组数据类型,拆分字符串,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用MYSQL中的函数,我有另一个表中的逗号分隔字符串(1,22,344,55),如何在MYSQL中将其拆分为数组(不是temp_table).另外,MYSQL中有类似的函数可以执行foreach()吗?

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不包含拆分定界字符串的函数.但是,创建自己的函数非常容易.

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, '');

用法

SELECT SPLIT_STR(string, delimiter, position)

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

这篇关于数组数据类型,拆分字符串,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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