Where Mysql search by full word

Asked

Viewed 194 times

4

I have a database and would like to know how I do to search for the data by typing the full result using Where

Example:

ID  NOME  CODIGOS
1  | Joao   | 9714,51,100
2  | Maria  | 50,9714,88100

Ai wanted to return a Where searching for CODE

Search: 100

Show: Joao

Search: 9714

Show: Joao, Maria

I did so:

SELECT * FROM usuarios WHERE codigo like '%100%'

But the 100 searched ta showing all the results that has 100, then returns 88100 and 100

What can I be doing?

  • if a reply served you then mark it as accepted, see how and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/6985#6985

1 answer

4


FIND_IN_SET - Serves to locate data in a table field that has comma separated values.

SELECT * FROM nomeTabela WHERE FIND_IN_SET('valorBuscado', nomeColuna);

Example table used:

tabela

SELECT * FROM usuarios WHERE FIND_IN_SET(100, codigo);

Upshot

resultado select

  • Nice! A test goes there to complement.... http://sqlfiddle.com/#! 9/9760cf/14

  • 1

    If the result of FIND_IN_SET and greater than 0 (zero), because this function only returns the input of the string 9714 in the value string separated by comma. It would look where FIND_IN_SET('9714', nomeColuna) > 0;

  • It worked right, obg.. did not know this function find_in_set

  • @Rodrigok. B is not required if the result is 0 it will function as the false and if it’s different it will work like true.

  • @Lucasmoraes, since solved, marks the answer as accepted ;)

Browser other questions tagged

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