watercolour watercolour - 1 year ago 76
SQL Question

Selecting rows where phrase is in different columns

I've tried finding an answer to this, but I've had no luck.

I want to select all rows that contain a string

in at least one column.

This is my SQL query right now:
SELECT * FROM catalog WHERE name LIKE '%$search%'

I'd like to search some of the other columns (brand, model, category, subcategory, stock, year) as well as the name column. Is there any way I can search all of these columns in one SQL query?

Answer Source
SELECT * FROM catalog 
WHERE name LIKE '%$search%' 
  OR brand LIKE '%$search%' 
  OR model LIKE '%$search%'
  OR category LIKE '%$search%'
  OR subcategory LIKE '%$search%'
  OR stock LIKE '%$search%'
  OR year LIKE '%$search%'

You just need an OR condition so that as long as at least one of the columns you'd like to match meets your search requirement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download