I am running through a dataset with a cursor.
In each cursor pass a string gets split up using a stored procedure which writes the single elements in a temporary table SplitValues, one value per row. Each dataset also has a unique ID ds_id
SplitValues
value |
--------+
Item1 |
Item2 |
Item3 | `
I also have a reference table which has IDs for each of the items the split function could possibly produce:
classes
id | class |
---+--------
5 | Item1 |
6 | Item2 |
7 | Item3 |
8 | Item4 |
9 | Item5 |
What I now want to do is to go through all rows of SplitValues and write them to a table to store the data, however using the ID of the class and not the class itself to save storage:
storage
ds_id | class_id |
------+-----------
0 | 5 |
0 | 6 |
0 | 7 |
This should be fairly simple but I am failing. I did multiple attempts, one was the following cursor loop:
FETCH cur1 INTO ds_id, string;
CALL `Split_String`(string, ',');
INSERT INTO storage (ds_id, class_id)
SELECT ds_id,
(SELECT classes.id FROM classes WHERE class = SplitValues.value ) as class_id ;
[...]
Error produced Unknown column 'SplitValues.value' in 'where clause'
I have searched a lot and think my problem is somehow related to aliases but was unable to find a solution.
Try this
Here is sqlfiddle