Mysql - check occurrence in all fields

Asked

Viewed 280 times

1

How to check the occurrence of a string in any field?

Nothing specific like WHERE campo1 = "abcd", since I want to check the occurrence in any field.

which is something different also from the extensive OR, sweep each field, something like : WHERE campo1 = "abcd" OR campo2 = "abcd" OR campo3 = "abcd",..

I think there’s another alternative besides this last one, yes?

  • Says Alexandre, all good? Look, I don’t know if it’s me, but I got a little lost in your doubt, could explain us better?

  • I found a way to do it on the Oracle, I don’t know where from, but I thought it was Oracle

  • @juniorb2ss Yes, instead of searching the database in some specific field, such as WHERE campo1 = 'valorqueeuprocuro', i would like to search in all fields of my table and not only in field1, I know that through the OR, I can repeat this same instruction for each field, but this would make the query great, I believe there is some simplified way.

  • 1

    No global method is possible, but there are some ways. http://stackoverflow.com/questions/3797906/mysql-query-for-searching-through-all-the-fields http://stackoverflow.com/questions/639531/search-in-all-fields-from-every-table-of-a-mysql-database http://stackoverflow.com/questions/639531/search-in-all-fields-from-every-table-of-a-mysql-database

1 answer

1


If it is possible to use a programming language, this task is not so complicated. The idea is to make two queries first by picking all the fields of a given table (maybe the type matters), then in the main query make a in() invertido instead of passing values to it pass the column list.

Ex:

Returns the column names.

SELECT column_name FROM information_schema.columns WHERE table_name = 'tabela'

The second consultation would be:

SELECT * FROM tabela where 'valor' IN(nome, email, endereco, outra_coluna)

Browser other questions tagged

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