SQL is ignoring AND statement when I add an OR statement?

Discussion in 'MySQL' started by amaze, Sep 21, 2007.

  1. #1
    Hi,

    Can't work this one out! When I perform the query below all the results comes back as expected:

    SELECT title, fkingredienttypeId, pkingredientId, subtitle 
    price, weightType
    FROM dingredients i
    WHERE i.title LIKE '%bluefoo%' 
    AND fkIngredientTypeId NOT IN (22,24,26) 
    AND disabled = 0
    ORDER BY title
    
    Code (markup):
    However when I add the OR statement (OR i.subtitle LIKE 'bluefoo') it then ignores the two AND statements following which obviously brings results back I don't want to show! Any ideas why this is happening?

    SELECT title, fkingredienttypeId, pkingredientId, subtitle 
    price, weightType
    FROM dingredients i
    WHERE i.title LIKE '%bluefoo%' OR i.subtitle LIKE 'bluefoo'
    AND fkIngredientTypeId NOT IN (22,24,26) 
    AND disabled = 0
    ORDER BY title
    Code (markup):
    By the way its a mySQL db and "bluefoo" is the random search term. Any help would be be great!

    Cheers
     
    amaze, Sep 21, 2007 IP
  2. meetgs

    meetgs Active Member

    Messages:
    957
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    70
    #2
    use ( )

    SELECT title, fkingredienttypeId, pkingredientId, subtitle 
    price, weightType
    FROM dingredients i
    WHERE (i.title LIKE '%bluefoo%' OR i.subtitle LIKE 'bluefoo')
    AND fkIngredientTypeId NOT IN (22,24,26) 
    AND disabled = 0
    ORDER BY title
    Code (markup):
     
    meetgs, Sep 21, 2007 IP
    amaze likes this.
  3. amaze

    amaze Active Member

    Messages:
    594
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thats worked a treat - rep for you - thanks!
     
    amaze, Sep 21, 2007 IP
  4. meetgs

    meetgs Active Member

    Messages:
    957
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    70
    #4
    meetgs, Sep 21, 2007 IP