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. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to running this query:

    LEFT JOIN `default_profiles` ON `default_profiles`.`user_id` = `default_users`.`id` 
    `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, any advice?

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

I optimized your query. I use IN instead OR because your point to the same field default_users.group_id and I added the brackets after the AND with ( LIKE '%polar%' OR default_users.username LIKE '%polar%'). You can use || instead OR (works the same).

FROM test.default_users 
LEFT JOIN default_profiles  ON (default_profiles.user_id =
WHERE default_users.group_id IN (3,4,5)
    AND ( LIKE '%polar%' 
    OR default_users.username LIKE '%polar%')
share|improve this answer

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 = WHERE (default_users.group_id = '3' OR default_users.group_id = '4' OR default_users.group_id = '5') AND ( LIKE '%polar%' OR default_users.username LIKE '%polar%') ORDER BY DESC

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

Your Answer


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.