Search using a comma separated string

So, I think I didn’t fully understand how FIND_IN_SET works

SELECT
    u.*, p.*
FROM
    users u
INNER JOIN profiles p ON p.user_id = u.id
WHERE
FIND_IN_SET('1,4,7', p.fruits)

This does not work as I thought.

1,4,7 are fruits selected by the user to search

p.fruits might look something like this: 1,2,3,4,5,6,7 or 5,6,7 or 1,6,7 etc.

Basically, I want to find entries if any of the values ​​in the first argument matches any of the values ​​in the second argument.

Is it possible?

+3
source share
2 answers

if your p.fruits column is varchar (which is not suitable for this situation, but if so), your query will look like

where …  ( concat(',', p.fruits , β€˜,’) like β€˜%,1,%’  
or concat(',', p.fruits , β€˜,’) like β€˜%,4,%’  or concat(',',p.fruits , β€˜,’) like β€˜%,7,%’ ) ... 

, . , .. user_fruits (fk_user_id int, fruit_id int) user_fruits

0

FIELD .

FIELD(p.fruits, 1,4,7)

:

10 MySQL ( )

0

All Articles