I have a table homes with (country,city,area,published) fields. I have a search form where someone can enter country or city or area. I want to get all homes which are published = 1 and any of the search terms matches any of their fields.
This is what I have so far:
SELECT * FROM homes
WHERE published = 1
AND
LOWER(country) LIKE '$search%'
OR
LOWER(city) LIKE '$search%'
OR
LOWER(area) LIKE '$search%'
The problem is that it return homes that have published = 0...
解决方案
I would suggest parentheses.
SELECT * FROM homes
WHERE published = 1
AND
(
LOWER(country) LIKE '$search%'
OR
LOWER(city) LIKE '$search%'
OR
LOWER(area) LIKE '$search%'
)