Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm trying to running this query:

SELECT * 
FROM 
    (`default_users`) 
    LEFT JOIN `default_profiles` ON `default_profiles`.`user_id` = `default_users`.`id` 
WHERE 
    `default_users`.`group_id` = '3' 
    OR `default_users`.`group_id` = '4' 
    OR `default_users`.`group_id` = '5' 
    AND `default_users`.`email` LIKE '%polar%' 
    OR `default_users`.`username` LIKE '%polar%' 
ORDER BY `default_users`.`id` DESC

But get unwanted results and don't know the cause and by unwanted results means webmaster which I think isn like %polar%. Here are some sample data for testing http://pastebin.com/Fyx0TcGY, any advice?

share|improve this question
5  
Probably an order of operations problem... add brackets as necessary in the WHERE clause. –  Jon Seigel Aug 8 '12 at 16:22
add comment

1 Answer 1

Yes @jon-seigel you're right and thanks for your suggestions didn't see it before :( I add some parentheses and now it works see the right query below:

SELECT * FROM (default_users) LEFT JOIN default_profiles ON
default_profiles.user_id = default_users.id WHERE (default_users.group_id = '3' OR default_users.group_id = '4' OR default_users.group_id = '5') AND (default_users.email LIKE '%polar%' OR default_users.username LIKE '%polar%') ORDER BY default_users.id DESC

share|improve this answer
    
Otherwise, is group_id a string type? –  dezso Aug 8 '12 at 16:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.