"Simplified" search of the same value in different fields (in the same table)

2

Currently for what I mention I use something similar to this:

SELECT * FROM 'tabla1' WHERE 'c2' LIKE 'leonidas' OR 'c1' LIKE 'leonidas' 
OR 'c0' LIKE 'leonidas'

What I am interested in knowing is, if I can do some sort of grouping of all the fields where I am going to search to only have to put the "LIKE 'leonidas'" Only once.

The result that I am looking for is to show * the data of the table where at least 1 of the 3 fields (c0, c1 or c2) is the value leonidas.

Example of the data that the query would bring me:

 1.  id | name  | c1       | c2         |  c0
 2. 3278|   A   | leonidas |  null      |  gorgo 
 3. 3499|   F   | null     |  leonidas  |  leonidas
 4. 4402|   Y   | gorgo    |  plistarco |  leonidas
    
asked by rflores 23.08.2018 в 18:14
source

1 answer

0

You can do it with the grouping operator CONCAT() indicating inside the parentheses the names of the necessary columns; separated by commas and later with a single LIKE indicate the match you should find

SELECT * FROM tabla
WHERE (CONCAT_WS('-', c2, c1, c0) LIKE 'leonidas');

or you can indicate that the match is at the end of the string

SELECT * FROM tabla
WHERE (CONCAT_WS('-', c2, c1, c0) LIKE '%leonidas');

or you can indicate that the match is at the beginning of the string

SELECT * FROM tabla
WHERE (CONCAT_WS('-', c2, c1, c0) LIKE 'leonidas%');
    
answered by 23.08.2018 в 18:44