MySQL JOIN & Split String

I want to join two tables. You have no problem with this. I have a problem with a completely different one.

Here is my code:

SELECT * FROM `table1`
JOIN `table2` ON `table1.`field`=`table2`.`field`
...

The main problem is what table1.fieldis a comma separated string. Is there a good and quick way to split it?

Update
I found the Federico Cagnelutti feature

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, '');

Using:

SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;

Very useful, but an integer is required for the third parameter. In any case, I do not know the number of words separated by commas. Fabulously, this is up to 20 words, but I do not know how much.

Update 2 To clarify my question. Here is what I like (I know that the following query is incorrect):

SELECT * FROM `table1`
JOIN `table2` ON `table2`.`id` IN (`table1`.`field`)

3
: table1.field= '202, 185, 505', table2.field= 202

+3
1

, , , , , FIND_IN_SET?

SELECT * FROM `table1`
JOIN `table2` ON FIND_IN_SET( `table2`.`field`, `table1.`field` ) > 0

, .

Update:

, , REPLACE. , , , , . :

SELECT  *
FROM    table1 t1
JOIN    table2 t2
ON      FIND_IN_SET( t2.field, REPLACE( t1.field, ' ', '' ) ) > 0

64 , , - , .

+1

Source: https://habr.com/ru/post/1762154/


All Articles