Requirements: divide the column with the separators into rows (table).
Why? You can use all the functions of SQL Aggregate to process strings.
Working sqlfiddle
Related questions that use this code:
I decided to use:
integer table (integersequence), which when "internally joined" or "cross-joined" (the same thing) to another table will generate rows that will undoubtedly have a unique serial number starting with 1. This is just used what RDBMS engines should do.
why is the integer table ?: It is small and will be loaded into the cache. This is easy to understand.
Instead of having a lot of code in the request, which hides what it actually does. I use functions that perform one task. It simplifies the basic request and can be checked and verified separately. At the moment, I am concerned about the ease of maintenance and understanding.
An example here is for one line. However, it can easily be expanded to a table of split rows of different sizes. "Cross join" will create all possible index parameters, starting with 1 when joining integersequence (I really need to use a different name: - /).
So the request is:
SET @StrToParse = "asdasdwdfef,rgrgtggt,weef"; select integerseries.id, count_in_set(@StrToParse, ','), value_in_set(@StrToParse, ',', integerseries.id) from integerseries where integerseries.id <= count_in_set(@StrToParse, ',');
Output:
PositionOfString, CountOfCommaDelimitedStrings, StringAtThatPosition 1 3 asdasdwdfef 2 3 rgrgtggt 3 3 weef
Now, how do we get these values:
I decided to use two functions:
The names are associated with the mysql function 'FIND_IN_SET' .
1) COUNT_IN_SET function: returns the counter character delimited items in the column.
CREATE FUNCTION `COUNT_IN_SET`(haystack VARCHAR(1024), delim CHAR(1) ) RETURNS INTEGER BEGIN RETURN CHAR_LENGTH(haystack) - CHAR_LENGTH( REPLACE(haystack, delim, '')) + 1; END$$
2) Function VALUE_IN_SET : treats the delimited list as one based array and returns the value in the specified "index".
CREATE FUNCTION `VALUE_IN_SET`(haystack VARCHAR(1024), delim CHAR(1), which INTEGER ) RETURNS VARCHAR(255) CHARSET utf8 COLLATE utf8_unicode_ci BEGIN RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(haystack, delim, which), delim, -1); END$$
Intereseries table - see (Tally tables)
CREATE TABLE `integerseries` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=500 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; insert into `integerseries`(`id`) values (1); insert into `integerseries`(`id`) values (2); insert into `integerseries`(`id`) values (3); insert into `integerseries`(`id`) values (4); insert into `integerseries`(`id`) values (5); insert into `integerseries`(`id`) values (6); insert into `integerseries`(`id`) values (7); insert into `integerseries`(`id`) values (8); insert into `integerseries`(`id`) values (9); insert into `integerseries`(`id`) values (10);