Splitting strings using MySQL stored procedures

Source: Internet
Author: User

(EXT) http://tec.5lulu.com/detail/104krn1e6p2w78d77.html

An existing string, such as Apple,banana,orange,pears,grape, to be separated by commas (,):

Apple
Banana
Orange
Pears
Grape

Then use the Where in () method to query.

1, the specific function:

  1. # function: Func_split_totallength
  2. DELIMITER $$
  3. DROP function IF EXISTS ' func_split_totallength ' $$
  4. CREATE definer=' root '@'% ' FUNCTION ' func_split_totallength '
  5. (f_string varchar (+),f_delimiter varchar(5)) RETURNS int( One )
  6. BEGIN
  7. # Calculates the total length of the incoming string
  8. return 1+ (length(f_string) - length(replace(f_ String,f_delimiter," ));
  9. END$$
  10. DELIMITER;
  1. # function: Func_split
  2. DELIMITER $$
  3. DROP function IF EXISTS ' func_split ' $$
  4. CREATE definer=' root '@'% ' FUNCTION ' func_split '
  5. (f_string varchar (+),f_delimiter varchar(5),f_order int ) RETURNS varchar(255) CHARSET UTF8
  6. BEGIN
  7. # Splits the passed-in string, returning the new string after splitting
  8. DECLARE result varchar(255) default ';
  9. set Result = reverse(substring_index (reverse (substring_index (f) _string,f_delimiter,f_order)),f_delimiter,1);
  10. return result;
  11. END$$
  12. DELIMITER;
  1. # Stored Procedure : splitstring
  2. DELIMITER $$
  3. DROP PROCEDURE IF EXISTS ' splitstring ' $$
  4. CREATE PROCEDURE ' splitstring '
  5. (in f_string varchar (+), inf_delimiter varchar(5))
  6. BEGIN
  7. # Split Results
  8. DECLARE CNT int default 0;
  9. DECLARE i int default 0;
  10. Set cnt = func_split_totallength(f_string,f_delimiter);
  11. DROP TABLE IF EXISTS ' tmp_split ';
  12. Create temporary table ' Tmp_split ' (' status ' varchar(+) not null ) DEFAULT CHARSET=UTF8;
  13. While I < cnt
  14. Do
  15. set i = i + 1;
  16. Insert into tmp_split(' status ') values (func_split(f_string ,f_delimiter,i));
  17. End while;
  18. END$$
  19. DELIMITER;

2. Whether the test can be successfully segmented

    1. Call splitstring("Apple,banana,orange,pears,grape",",");
    2. Select * from tmp_split;


The results of the operation are as follows, stating that the segmentation succeeded:

Mysql> call Splitstring ("Apple,banana,orange,pears,grape", ",");
SELECT * from Tmp_split;
Query OK, 1 row affected

+--------+
| Status |
+--------+
| Apple |
| Banana |
| Orange |
| Pears |
| Grape |
+--------+
5 rows in Set

Mysql>

3. Apply where in () query

      1. # first pass in string, save in temp table tmp_split
      2. call Splitstring ( "Apple,banana,orange,pears,grape" ,
      3. # use the results of the query as conditions for other queries using
      4. select * fruit where in ( Span class= "KWD" >select * from Tmp_split

Splitting strings with MySQL stored procedures

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.