Use MySQL stored procedure to split strings

Source: Internet
Author: User

The MySQL stored procedure can be used to split strings. The following describes the usage of this MySQL Stored Procedure for your reference.

A string, such as apple, banana, orange, pears, and grape, must be separated by commas (,):

Apple
Banana
Orange
Pears
Grape

Then you can use the where in () method to query.

1. Specific functions:
 
 
  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 (1000), f_delimiter varchar (5) RETURNS int (11)
  6. BEGIN
  7. # Calculate the total length of the input 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 (1000), f_delimiter varchar (5), f_order int) RETURNS varchar (255) CHARSET utf8
  6. BEGIN
  7. # Split the input string and return the new string after splitting.
  8. Declare result varchar (255) default '';
  9. Set result = 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 (1000), IN f_delimiter varchar (5 ))
  6. BEGIN
  7. # Splitting result
  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 (128) 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. Test whether segmentation is successful
 
 
  1. call splitString("apple,banana,orange,pears,grape",","); 
  2. select * from tmp_split; 

The running result is as follows, indicating that the splitting is successful:

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. Application where in () Query
 
 
  1. # Input a string, split it, and save it in the temporary table tmp_split.
  2. Call splitString ("apple, banana, orange, pears, grape ",",");
  3. # Use the query result as a condition for other queries
  4. Select * from fruit where in (select * from tmp_split );

The preceding section describes how to use the MySQL stored procedure to split strings.

MySQL string addition function usage example

MySQL string truncation Function Method

MySQL string segmentation implementation

In-depth study of MySQL result strings

How to quickly enable MySQL slow log query

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.