How to find a word in 10 tables in Mysql

Asked

Viewed 105 times

0

I am making a form for a search, and when searching for something, I intend to return several values of various tables that exist.

As such, I would like to know the easiest way to do a query, in which it is not too heavy and can read all tables with several different fields.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

1 answer

1

One way is to use the clause UNION and join search results:

SELECT 'TABELA 1' AS origem
       t1.campo1,
       t1.campo2
  FROM tabela1 t1
 WHERE t1.campo1 like '%texto%'
    OR t1.campo2 like '%texto%'
UNION
SELECT 'TABELA 2' AS origem
       t2.campo1,
       t2.campo2
  FROM tabela2 t2
 WHERE t2.campo1 like '%texto%'
    OR t2.campo2 like '%texto%'
...
UNION
SELECT 'TABELA 10' AS origem
       t10.campo1,
       t10.campo2
  FROM tabela3 t10
 WHERE t10.campo1 like '%texto%'
    OR t10.campo2 like '%texto%'

Browser other questions tagged

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