How to find an item in a comma-separated field?

Asked

Viewed 715 times

2

I have a field in a table with data separated by 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 it is in 2,3,4,5. Even using LIKE. I guess why I need to ride a array before the query. How to find data in PHP + Mysql?

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

  • 3

    You can help http://answall.com/q/1788

1 answer

1


You can use the function FIND_IN_SET():

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

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

  • I have now seen the link placed by @rray in the comments that effectively answers the question with the appropriate solution.

  • Okay, I’ll try this solution, then I’ll let you know if it worked for what I want! Thanks for while!

  • 2

    Perfect! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.