Soldato
- Joined
- 24 Nov 2002
- Posts
- 16,378
- Location
- 38.744281°N 104.846806°W
For whatever reason I have a database that contains a foreign key, but these keys are comma seperated within the field.
E.g.
Table1:
animals |blah | blah
1, 3, 5 |blah | blah
Table2:
ref| name | colour | size
1 | dog | black | large
2 | cat | pink | small
3 | ...... | ...... | ......
4 | ...... | ...... | ......
5 | ...... | ...... | ......
I've played around, but can only get the first and last value (so 1 and 5 above), using:
E.g.
Table1:
animals |blah | blah
1, 3, 5 |blah | blah
Table2:
ref| name | colour | size
1 | dog | black | large
2 | cat | pink | small
3 | ...... | ...... | ......
4 | ...... | ...... | ......
5 | ...... | ...... | ......
I've played around, but can only get the first and last value (so 1 and 5 above), using:
Code:
SELECT *
FROM `Table1`
JOIN `Table2` ON SUBSTRING_INDEX(animals, ',', 1 ) = `Table2`.ref
UNION
SELECT *
FROM `Table1`
JOIN `Table2` ON SUBSTRING_INDEX(animals, ',', -1 ) = `Table2`.ref