2

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...

2 Answers 2

3

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%'
)
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT * 
FROM   homes 
WHERE  published = 1 
       AND ( country LIKE '$search%' 
              OR city LIKE '$search%' 
              OR area LIKE '$search%' ) 

Like already does the case-insensitive stuff so you don't need it.

The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a. http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

1 Comment

My default character set is UTF-8 and collation is utf_general_ci. I think I need the LOWER()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.