Search all columns of a table using a single where condition with single keyword in mysql
I have a table which consists of 64 different fields. i am going to search with a single keyword in it, Results should match the keyword from any field. Give some suggestions.
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.
you can use the where with multiple condition with OR
like
where
name = 'expected'
OR rate ='expected'
OR country ='expected'
I can't see a way around your query being simple but long:
SET @term = "Somesearch";
SELECT id, title FROM sometable WHERE
col1 LIKE '%@term%' OR
col2 LIKE '%@term%' OR
col3 LIKE '%@term%' ...;
Instead of using a MySQL variable, you can just use a language-specific variable but for the sake of examples, I thought I'd stick with MySQL itself.
The "..." is where you'd place the other 61 columns/fields.