Mysql uses the stored procedure to achieve a split-like effect.

Source: Internet
Author: User

Mysql uses the stored procedure to achieve the effect similar to split. Foreground: the keywords in the database are separated by ";", and the query result set is sorted in descending order according to the number of keyword frames, and display it on the page according to a single keyword. The following is the Stored PROCEDURE Java code create procedure 'proc _ split '(inputstring varchar (1000), delim char (1 )) not deterministic contains SQL SECURITY DEFINER COMMENT ''begin declare strlen int; declare last_index int; declare cur_index int; declare cur_char VARCHAR (200); declare len int; set cur_index = 1; set last_index = 0; set strlen = length (inputstring); drop temporary table if exists splittable; create TEMPORARY table splittable default charset = utf8 (id int AUTO_INCREMENT, value VARCHAR (20 ), primary key ('id'), unique key 'id' ('id'); WHILE (cur_index <= strlen) DO begin if substring (inputstring from cur_index for 1) = delim or cur_index = strlen then set len = cur_index-last_index-1; if cur_index = strlen then set len = len + 1; end if; insert into splittable ('value ') values (substring (inputstring from (last_index + 1) for len); set last_index = cur_index; end if; set cur_index = cur_index + 1; END; end while; /* test */call proc_split ('a; B; c; D', ';'); select * from splittable; it is only suitable for processing a single word, we may encounter a situation where all fields (each field may contain only one keyword or multiple keywords) are processed into one multi-keyword, with an SQL statement, you can select group_concat (name separator ';') as tags from tags where LENGTH (name)> 0 parameter description: LENGTH (name)> fields with 0 being null are not processed.

Related Article

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.