1
SELECT * FROM cars WHERE carBrand LIKE '%alfa%' OR carModel LIKE '%alfa%'

Everything i put after that sentence just gets ignored. I was hoping to be able to do this.

SELECT * FROM cars
WHERE carBrand LIKE '%alfa%'
OR carModel LIKE '%alfa%' AND carType = 'truck'

This just gets ignored.

AND carType = 'truck'

Why?

pirho
  • 11,565
  • 12
  • 43
  • 70
  • First thing I would try is `SELECT * FROM cars WHERE (carBrand LIKE '%alfa%') OR (carModel LIKE '%alfa%' AND carType = 'truck')` – 321zeno Mar 20 '14 at 13:58

1 Answers1

11

Use parentheses

SELECT * FROM cars 
WHERE (carBrand LIKE '%alfa%' OR carModel LIKE '%alfa%')
AND carType = 'truck'

AND binds stronger than OR. It's called Operator Precedence. Without parentheses your query compiles to

SELECT * FROM cars 
WHERE carBrand LIKE '%alfa%' 
OR (carModel LIKE '%alfa%' AND carType = 'truck')
juergen d
  • 201,996
  • 37
  • 293
  • 362