Multiple WHERE (=, LIKE)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Multiple WHERE (=, LIKE)

Post by AliasBDI »

I need to have a query that checks for one field to be equal while other fields be LIKE. Here is my query, it is disregarding the = check.

Code: Select all

"SELECT * FROM con_articles WHERE active = 'Y' AND common LIKE " . $search1 . " OR common LIKE " . $search2 . " OR common LIKE " . $search3 . ""
Any ideas?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

OR operator has higher precedence. Use parenthesis to override the precedence, like this:

Code: Select all

SELECT 
   * 
FROM 
   con_articles 
WHERE 
   active = 'Y' 
   AND (
       common LIKE '$search1'
       OR common LIKE '$search2'  
       OR common LIKE '$search3'
   )
Post Reply