Search all columns of a table using a single where condition with single keyword in mysql

SELECT * FROM `some_table`
WHERE
CONCAT_WS('|',`column1`,`column2`,`column3`,`column4`,`column64`) # single condition, many columns
LIKE '%VT%'

Voila.

The ‘|’ separator, by the way, is to prevent you finding coincidental matches where, e.g., column1 ends in ‘V’ and column2 starts with ‘T’, which would give you a false positive in a search for “VT”.

I’m not sure if the above method is faster than the OR method (I would guess they’re the same speed) , but it definitely involves less typing if you’re writing the query by hand.

Leave a Comment