Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 766
R J. Avatar asked Sep 23 '12 06:09

R J.


People also ask

How can you list all columns for a given table in MySQL?

You can list a table's columns with the mysqlshow db_name tbl_name command. The DESCRIBE statement provides information similar to SHOW COLUMNS .


2 Answers

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.

like image 181
Buttle Butkus Avatar answered Sep 22 '22 03:09

Buttle Butkus


you can use the where with multiple condition with OR

like

where name = 'expected' OR rate ='expected'  OR country ='expected' 
like image 23
NullPoiиteя Avatar answered Sep 21 '22 03:09

NullPoiиteя