Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like statement across multiple columns in SQL

I'm trying to query a like statement across multiple columns. I have the following search terms: 'dog' 'cat' 'rabbit' 'gerbil' 'guinea pig' 'hamster'

and I need search for these terms or terms LIKE these in the 'animals' table which has about 40 different columns. I am aware I can do the like statement by doing

 Select * from animals where [animalscolumn] like ('%dog%') or like ('%cat%') or like ('%gerbil%') or like ('%hamster%') or like ('%guinea pig%')

However the 'animalscolumn' isn't the only column I need to run the 'LIKE' statement across. I have to search for these terms in about 40 columns. Would anyone happen to know how? Thanks!

like image 983
AneeshaKK Avatar asked Jun 29 '26 09:06

AneeshaKK


1 Answers

multiple like statements can not be used with or directly. You have to use column name for each like statement.

Use multiple like as mentioned below.

Select * 
from animals 
where 
(
[animalscolumn] like ('%dog%') or 
[animalscolumn] like ('%cat%') or 
[animalscolumn] like ('%gerbil%') or 
[animalscolumn] like ('%hamster%') or 
[animalscolumn] like ('%guinea pig%')
)
like image 105
maulik kansara Avatar answered Jul 02 '26 00:07

maulik kansara