MySQL 直接用SQL分割字符串到数组

承接上文:MySQL  中在不编写存储过程或函数的前提下,利用SQL 语句拆分一个字符串到数组

主要思路其实一致:够造序列,然后结合MySQL的SUBSTRING_INDEX 函数的特性分割字符串。

SET @str ="a,b,c,d,e,f,g,h,i,j,k" ;

SET @delim="," ;

SET @str_length=length(@str)- length( REPLACE (@str,@delim,‘‘) )+1 ;

SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX( @str, @delim, tmp.seq),@delim, - 1) as str_split
FROM
(
 SELECT
s1.i + s2.i + s3.i + s4.i + s5.i + s6.i + s7.i + s8.i + s9.i+1 AS seq
FROM    (  SELECT  0 AS i  UNION ALL SELECT   1  AS i  ) s1
                    CROSS JOIN ( SELECT   0 AS i UNION ALL  SELECT  2 AS i  ) s2
                    CROSS JOIN ( SELECT   0 AS i  UNION ALL SELECT   4 AS i  ) s3
                    CROSS JOIN ( SELECT   0 AS i  UNION ALL SELECT   8 AS i  ) s4
                    CROSS JOIN ( SELECT   0 AS i   UNION ALL SELECT   16 AS i  ) s5
                    CROSS JOIN ( SELECT   0 AS i   UNION ALL SELECT   32 AS i  ) s6
                    CROSS JOIN ( SELECT   0 AS i  UNION ALL SELECT   64 AS i  ) s7
                    CROSS JOIN ( SELECT   0 AS i   UNION ALL SELECT   128 AS i  ) s8
                    CROSS JOIN ( SELECT   0 AS i  UNION ALL  SELECT   256 AS i   ) s9
ORDER BY seq
LIMIT 400
) tmp
WHERE
tmp.seq <= @str_length 
时间: 2024-11-08 19:03:52

MySQL 直接用SQL分割字符串到数组的相关文章

直接用SQL语句把DBF导入SQLServer

直接用SQL语句把DBF导入SQLServer   在SQLServer中执行 SELECT * into bmk FROM OpenDataSource( ’Microsoft.Jet.OLEDB.4.0’,’Data Source=”e:share”;User ID=Admin;Password=;Extended properties=dBase 5.0’)...bmk 这样就可以把e:share中的bmk.dbf表导入到Sqlserver中, 速度是最快的 ---------------

javascript 中 split 函数分割字符串成数组

分割字符串成数组的方法有很多,不过使用最多的还是split函数 <script language="javascript"> str="2,2,3,5,6,6"; //这是一字符串 var strs= new Array(); //定义一数组 strs=str.split(","); //字符分割 for (i=0;i<strs.length ;i++ ) { document.write(strs[i]+"<b

java类中与js中split分割字符串转数组区别

java类中,以","分割,将字符串转化为数组 String str = "a,b,c,"; String[] arr = str.split(","); System.out.println(arr.length); 输出长度为3,arr[0]="a"; arr[1]="b"; arr[2]="c"; js中,以","分割,将字符串转化为数组 var str = &

js split 的用法和定义 js split分割字符串成数组的实例代码

<script language="javascript"> str="2,2,3,5,6,6"; //这是一字符串 var strs= new Array(); //定义一数组 strs=str.split(","); //字符分割 for (i=0;i<strs.length ;i++ ) { document.write(strs[i]+"<br/>"); //分割后的字符输出 } <

js split分割字符串成数组

str = "2,2,3,5,6"; //这是一字符串 var strs = new Array(); //定义一数组 strs = str.split(","); //字符分割 for (i=0; i<strs.length; i++ ){ document.write(strs[i]+"<br/>"); //分割后的字符输出 } 输出结果: 2 2 3 5 6 原文地址:https://www.cnblogs.com/QW-

Sql 分割字符串

/* 分割符为',' */ declare @depid varchar(100) declare @location int declare @length int --示例字符串 set @depid = '13434,32,34,567,12,9998,' set @location = 1 set @length = 0 while @location <> 0 begin set @location = charindex(',', @depid, @location) if @lo

解决 微信包含emoji表情的昵称,直接用sql语句可以写入而在yii2框架写却写不成功的 问题

背景: 首先是emoji表情写入不成功,在网上查了许多资料,包括配置mysql,重启mysql等等,这样之后发现还是写入失败. 将sql语句复制出来,直接粘贴到mysql客户端执行,发现没问题.而通过代码写入却有问题 解决方案: 在config/db.php中将charset = 'utf8'改为charset='utf8mb4' 原生php也类似,可以检查代码中的charset 原文地址:https://www.cnblogs.com/roggeyue/p/10811993.html

SQL分割字符串函数

SQL创建函数 数据库名->可编程性->函数->表值函数 右键创建“多语句表值函数” CREATE FUNCTION [dbo].[fun_splitstr](@str VARCHAR(MAX)) RETURNS @temp TABLE(id INT) AS BEGIN DECLARE @ch AS VARCHAR(100) SET @str+=',' WHILE(@str<>'') BEGIN SET @ch = LEFT(@str,CHARINDEX(',',@str,1

sql分割字符串

DECLARE @str NVARCHAR(MAX); SET @str = 'aa|bb|cc|dd'; DECLARE @n NVARCHAR(100) SET @str += '|' WHILE LEN(@str) > 1 BEGIN SET @n = LEFT(@str, CHARINDEX('|', @str)) SET @str = RIGHT(@str, LEN(@str) - LEN(@n)) SET @n = REPLACE(@n, '|', '') SELECT @n END