Jump to content

Multiple COLUMN single LIKE statement?


monkeytooth

Recommended Posts

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%'

Link to comment
Share on other sites

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%')

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.