Click to See Complete Forum and Search --> : Simulating split() by dynamically appending new rows


Ultimater
10-23-2007, 09:00 PM
MySQL has the power to execute beautiful queries like:

SELECT 'a','b' UNION SELECT 'c','d'

Where commas add new rows and UNIONs add new columns or groups.
We encounter an obstacle since MySQL doesn't provide support for a native SPLIT() function.
I was wondering if someone could think of a way to hard code-in a simulation of split by dynamically looping through a string and appending new rows to a group for output. The number of rows would be relative to the number of matches of a comma character contained within a scalar comma-delimited string.

Given a simple table like:

CREATE TABLE `tbl` (`fld` TEXT NOT NULL) TYPE = MYISAM
INSERT INTO `tbl` ( `fld` ) VALUES ( 'a,b' )
INSERT INTO `tbl` ( `fld` ) VALUES ( 'foobar,FTP Operation Over Big Address Records' )


I could write a pointless query like:

SELECT SUBSTRING(`fld`,1,1),SUBSTRING(`fld`,3,1) FROM `tbl` LIMIT 1


But to simulate a result like:
SELECT 'foobar','FTP Operation Over Big Address Records'
it would get more complex like:

SELECT SUBSTRING(`fld`,1,LOCATE(',',`fld`)-1),SUBSTRING(`fld`,LOCATE(',',`fld`)+1,CHAR_LENGTH(`fld`)-LOCATE(',',`fld`)) FROM `tbl` LIMIT 1,1



QUESTION:
I was wondering if you guys could think of a way to use BENCHMARK() to loop through a comma-delimited string. Creating a FUNCTON is one way but I was more interested in a clever way to put BENCHMARK to use.