I want to JOIN two table. Have no problem with that. I have problem with quite different thing.
Here's my code:
SELECT * FROM `table1`
JOIN `table2` ON `table1.`field`=`table2`.`field`
...
The main issue is that table1.field
is a string, comma-separated. Is there any good and fast way to split it?
Update
I found a function by Federico Cagnelutti
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
Usage:
SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;
Quite useful but it requires position integer as third parameter. Anyway, I do not know the amount of comma-separated words. Bascially, it's up to 20 words, but I don't know how many.
Update 2 To clarify my question. Here's what I like to have (I know the following query is incorrect):
SELECT * FROM `table1`
JOIN `table2` ON `table2`.`id` IN (`table1`.`field`)
Update 3
Example strings: table1.field
= '202, 185, 505', table2.field
= 202