Mariadb version 10.3.34. SQL to create the example tables is on this gist.
I have to work with a foreign database on which I have no control. So suggestions to modify the structure of the DB are, sadly, unacceptable. I can add functions, though.
Now, in this database, things can have from 0 to n colors, and the color references are coded as a string of all values joined by a |
char, like this :
+----------------------+
| things |
| name (pkey)| colorsid|
+------------+---------+
| 'door' | '20|5' |
| 'car' | '10' |
| 'hammer' | null |
| 'box' | '5' |
+------------+---------+
+------------------+
| colors |
| id | color |
+------+–––––––––––+
| 5 | 'red' |
| 10 | 'blue' |
| 20 | 'black' |
+------+–––––––––––+
So the door is black and red, the car is blue, the hammer has no color, and the box is red.
How to “JOIN” on each color code for each thing ?
I'd like a query whose result is this :
+---------------------------+
| thingsandcolors |
| name | colornames |
+------------+--------------+
| 'door' | 'black|red' |
| 'car' | 'blue' |
| 'hammer' | null |
| 'box' | 'red' |
+------------+--------------+
Performance is not an issue (to a reasonable extent, of course). The DB is expected to contain at most a few tens of colors, and no more than 10 000 things.