Page 1 of 1

IF statement = OR AND OR

Posted: Tue Nov 03, 2009 3:14 pm
by amanxman
Hey,

I have a PHP statement which basically says:

variableA = 1 OR variableA = 2 AND variableB = Z AND variableC = N OR variableC = M

actual statement is below, but the above is easier to read!:

Code: Select all

 
$where = "location = 'ALL NORTH' OR location = 'Andreas' OR location = 'Ballaugh' OR location = 'Bride' OR location = 'Jurby' OR location = 'Lezayre' OR location = 'Maughold' OR location = 'Ramsey' OR location = 'St Judes' OR location = 'Sulby' AND sales_types_available LIKE '%Buy%' AND show_on_website = '1' AND development_type = 'HOUSING' OR development_type = '3 Bed House' OR development_type = '4 Bed House' OR development_type = '5 Bed House +' OR development_type = '1 Bed Bungalow' OR development_type = '2 Bed Bungalow' OR development_type = '3 Bed Bungalow' OR development_type = '4 Bed Bungalow'";
 
the above statement results in variable A being ignored if varaible C matches - because of the OR
i.e
Variable A = 1 or 2
AND Varaible B = Z
OR Variable C = M or N


what I want is variableA to apply, and then variableC to apply seperately
so:
Variable A = 1 or 2
AND Varaible B = Z
AND Variable C = M or N

is that possible? or do i need to split it down to two statements?

Ta


SOLVED with parenthesis

Code: Select all

 
  
 $where = "(location = 'ALL NORTH' OR location = 'Andreas' OR location = 'Ballaugh' OR location = 'Bride' OR location = 'Jurby' OR location = 'Lezayre' OR location = 'Maughold' OR location = 'Ramsey' OR location = 'St Judes' OR location = 'Sulby') AND (sales_types_available LIKE '%Buy%') AND (show_on_website = '1') AND (development_type = 'APARTMENTS' OR development_type = '1 Bed Apartment' OR development_type = '2 Bed Apartment' OR development_type = '3 Bed Apartment +')";
 
simple!!!

Re: IF statement = OR AND OR

Posted: Fri Nov 06, 2009 2:54 am
by sureshmaharana
Try this:

Code: Select all

 
$where = "(location = 'ALL NORTH' OR location = 'Andreas' OR location = 'Ballaugh' OR location = 'Bride' OR location = 'Jurby' OR location = 'Lezayre' OR location = 'Maughold' OR location = 'Ramsey' OR location = 'St Judes' OR location = 'Sulby') AND (sales_types_available LIKE '%Buy%' AND show_on_website = '1') AND (development_type = 'HOUSING' OR development_type = '3 Bed House') OR development_type = '4 Bed House' OR development_type = '5 Bed House +' OR development_type = '1 Bed Bungalow' OR development_type = '2 Bed Bungalow' OR development_type = '3 Bed Bungalow' OR development_type = '4 Bed Bungalow')";
 

Re: IF statement = OR AND OR

Posted: Fri Nov 06, 2009 3:50 am
by VladSun
Use a single IN operator instead of multiple OR operations
http://dev.mysql.com/doc/refman/5.0/en/ ... unction_in