mysqldude

Splitting fields

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
SELECT from tdata before calling the procedure (expect 1146)
original post
✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146
Cursor + LOCATE loop into a temporary table (original)
original post
✓✓✓✓✓✓✓✓✓
Numbers table + SUBSTRING_INDEX, no procedure (portable)
modern rewrite
✓✓✓✓✓✓✓✓✓
Recursive CTE (MySQL 8.0+, MariaDB 10.2+)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
JSON_TABLE (MySQL 8.0.4+, MariaDB 10.6+)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
SELECT from tdata before calling the procedure (expect 1146) original post
MySQL 5.6✓ 11465.7✓ 11468.0✓ 11468.4✓ 11469✓ 1146
MariaDB 10.6✓ 114610.11✓ 114611.4✓ 114611.8✓ 1146
Cursor + LOCATE loop into a temporary table (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Numbers table + SUBSTRING_INDEX, no procedure (portable) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Recursive CTE (MySQL 8.0+, MariaDB 10.2+) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
JSON_TABLE (MySQL 8.0.4+, MariaDB 10.6+) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • Recursive CTE (MySQL 8.0+, MariaDB 10.2+) (MySQL 5.6, MySQL 5.7): error 1064: SELECT id, SUBSTRING_INDEX(attributes, ' at line 1
  • JSON_TABLE (MySQL 8.0.4+, MariaDB 10.6+) (MySQL 5.6, MySQL 5.7): error 1064: ' at line 3

Related errors: 1146

The original article

First published 2011-05-09. The text below is the original; the table above shows how it behaves on current versions.

It should be noted that anyone who needs this type of solution should look closely at their data model and have this corrected.

A question was raised where a field in the table contains a comma separated list of entries. These entries should be accessible. Through a set of queries. Let us call these comma separated values attributes. The data model should consist of a separate table with a unique identifier and the various attributes in question. But for argument sake let us imagine that this is not available and that we need to generate a temporary table consisting of these attribute values.

mysql> create table data (id int auto_increment primary key, attributes varchar(255));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into data(attributes) values ('One,Two,Three,Four'),('a,b,c,d,e'),('Dog,Cat,Horse');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from data;
+----+--------------------+
| id | attributes         |
+----+--------------------+
|  1 | One,Two,Three,Four |
|  2 | a,b,c,d,e          |
|  3 | Dog,Cat,Horse      |
+----+--------------------+
3 rows in set (0.00 sec)

Let us now create a stored procedure which takes each of these records and adds them to a temporary table that we have created.

drop procedure if exists sp_data;
delimiter $$
create procedure sp_data()
begin
	declare done int default 0;
	declare lattributes varchar(255);
	declare lid integer;
	declare idx integer;
	declare cdata cursor for select id, attributes from data;
	declare continue handler for not found set done := 1;
	create temporary table if not exists tdata(id int, attribute varchar(100));
	open cdata;
	read_loop: loop
		fetch cdata into lid, lattributes;
		if done then
			leave read_loop;
		end if;
		attr_loop: loop
			set idx := locate(',', lattributes);
			if idx > 0 then
				set @lattribute := substring(lattributes, 1, idx-1);
			else
				set @lattribute := lattributes;
			end if;
			INSERT INTO tdata(id, attribute) VALUES (lid, @lattribute);
			if idx = 0 then
				leave attr_loop;
			end if;
			set lattributes := trim(substring(lattributes, idx+1));
		end loop;
	end loop;
end;
$$
delimiter ;

As we have a temporary table the results are only visible for the duration of the session. Afterwhich these are removed by the MySQL server.

Calling this function on this table we get:

mysql> select * from tdata;
ERROR 1146 (42S02): Table 'test.tdata' doesn't exist
mysql> call sp_data;
Query OK, 1 row affected (0.00 sec)

mysql> select * from data;
+----+--------------------+
| id | attributes         |
+----+--------------------+
|  1 | One,Two,Three,Four |
|  2 | a,b,c,d,e          |
|  3 | Dog,Cat,Horse      |
+----+--------------------+
3 rows in set (0.00 sec)

mysql> select * from tdata;
+------+-----------+
| id   | attribute |
+------+-----------+
|    1 | One       |
|    1 | Two       |
|    1 | Three     |
|    1 | Four      |
|    2 | a         |
|    2 | b         |
|    2 | c         |
|    2 | d         |
|    2 | e         |
|    3 | Dog       |
|    3 | Cat       |
|    3 | Horse     |
+------+-----------+
12 rows in set (0.00 sec)