MySQL directly splits strings into arrays with SQL

Source: Internet
Author: User
Tags mysql in

To undertake the above: MySQL in the premise of not writing stored procedures or functions, the use of SQL statements to split a string into an array

The main idea is actually the same: enough to build a sequence, and then combine the features of MySQL's Substring_index function to split the string.

SET @str ="A,b,c,d,e,f,g,h,i,j,k";SET @delim="," ;SET @str_length=Length@str)-LengthREPLACE(@str,@delim,"') )+1 ;SELECTSubstring_index (Substring_index (@str,@delim, Tmp.seq),@delim,- 1) asStr_split from(  SELECTs1.i+s2.i+s3.i+S4.i+S5.i+S6.i+S7.i+S8.i+S9.i+1  asseq from(SELECT  0  asIUNION  All SELECT   1   asi) S1 Cross JOIN(SELECT   0  asIUNION  All  SELECT  2  asi) S2 Cross JOIN(SELECT   0  asIUNION  All SELECT   4  asi) S3 Cross JOIN(SELECT   0  asIUNION  All SELECT   8  asi) S4 Cross JOIN(SELECT   0  asIUNION  All SELECT    -  asi) S5 Cross JOIN(SELECT   0  asIUNION  All SELECT    +  asi) S6 Cross JOIN(SELECT   0  asIUNION  All SELECT    -  asi) S7 Cross JOIN(SELECT   0  asIUNION  All SELECT    -  asi) S8 Cross JOIN(SELECT   0  asIUNION  All  SELECT    the  asi) S9ORDER  bySeqlimit -) TMPWHERETmp.seq<= @str_length 

MySQL directly splits strings into arrays with SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.