How to find an item in a field separated by commas? [duplicate]

2

I have a field in a table with data separated by a comma: id_regioes: 1,2,3,4,5,6 .

When I do the query "SELECT * FROM 'tb_operadora' WHERE 'id_regioes' = 1" , or "= 6" returns ok, but not when they are in 2,3,4,5. Even using LIKE . I guess because I need to mount an array before the query . How do I find the data in PHP + MySQL?

When I search in this field separated by commas, it only finds items that fit the first and / or last record of the field.

    
asked by anonymous 23.03.2015 / 14:32

1 answer

1

You can use the FIND_IN_SET() :

SELECT *
FROM 'tb_operadora'
WHERE FIND_IN_SET( 3, 'id_regioes' )

What is effect is to find the value of the first parameter in a set defined in the second parameter.

    
23.03.2015 / 16:47