monkeytooth Posted March 15, 2011 Share Posted March 15, 2011 is there a way to do a Multiple Column LIKE statement with mySQL/PHP example: lets say I have four columns, and I want to run a LIKE '%kw%' for each of the four columns without haveing to do repeat likes. Concept of what I am trying to say.. TABLE_NAME (col1, col2, col3, col4) LIKE '%kw%' Quote Link to comment https://forums.phpfreaks.com/topic/230746-multiple-column-single-like-statement/ Share on other sites More sharing options...
The Little Guy Posted March 16, 2011 Share Posted March 16, 2011 select * from table where (col1 or col2 or col3 or col4 like '%apple%') Quote Link to comment https://forums.phpfreaks.com/topic/230746-multiple-column-single-like-statement/#findComment-1188061 Share on other sites More sharing options...
fenway Posted March 17, 2011 Share Posted March 17, 2011 select * from table where (col1 or col2 or col3 or col4 like '%apple%') No, that's wrong -- LIKE is not associative. select * from table where (col1 like '%apple%' or col2 like '%apple%' or col3 like '%apple%' or col4 like '%apple%') Quote Link to comment https://forums.phpfreaks.com/topic/230746-multiple-column-single-like-statement/#findComment-1188759 Share on other sites More sharing options...
jamesjmann Posted March 17, 2011 Share Posted March 17, 2011 select * from table where (col1 or col2 or col3 or col4 like '%apple%') No, that's wrong -- LIKE is not associative. select * from table where (col1 like '%apple%' or col2 like '%apple%' or col3 like '%apple%' or col4 like '%apple%') can you replace the or's with and's? furthermore, is it possible to rewrite the statement like this? SELECT * FROM table WHERE col1 LIKE '%apple%' AND col2 LIKE '%apple%' AND col3 LIKE '%apple%' AND col4 LIKE '%apple%' The reason I ask this, is because I seen somewhere else that you can use "AND", but after reading this thread, I'm confused. Is "OR" meant to search for a row that has one or more columns that match the input, and "AND" for searching for rows that have all columns matching all input? If so, is the above how you could write that? Quote Link to comment https://forums.phpfreaks.com/topic/230746-multiple-column-single-like-statement/#findComment-1188950 Share on other sites More sharing options...
fenway Posted March 29, 2011 Share Posted March 29, 2011 Yes, you can replace the ORs with ANDs -- of course, it means something different. And yes, that's correct -- you need ANDs if you want all 4 to match. Quote Link to comment https://forums.phpfreaks.com/topic/230746-multiple-column-single-like-statement/#findComment-1193912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.