Page 1 of 1

combining AND / OR statements

Posted: Sat Aug 13, 2005 7:05 pm
by taldos
Hey all,

Just a quick question regarding AND / OR statement. I am using the following code in my mysql query to find matching rows to the following query.

Code: Select all

$alert = "SELECT id, email, first_name, last_name, lead_alert FROM members WHERE 
(location LIKE '%".$country."%' OR location='All')
AND (industry LIKE '%".$industry."%' OR industry='All')";

$a_sql = mysql_query($alert)
	or die ("Could not select matching members");
No matter how many times I scratch my head I can't figure it out. the fields specified (industry and location) contain more than one word of text. I don't think this has anything to do with the problem since LIKE uses patter matching, but just thought I would ask.

I am pretty sure it has something to do with how I am using the 'OR' statement combined with AND. Any help is appreciated.

Cheers.

Posted: Sat Aug 13, 2005 7:12 pm
by feyd
you're requiring all records to match the inputs of $country and $industry or be 'All' ?

What's the logic you want? What are you currently getting in records? What do you want in records?

Posted: Sat Aug 13, 2005 7:30 pm
by taldos
If my table resembles the following:

Code: Select all

name     industry     location

tom        food          US
joe         finance     Japan
don         All            England
leon        fitness      US
mike        All            All
and I run a search for members whose industry= food/all AND country=US/all

the following records should be returned

Code: Select all

1. tom      food    us
2  mike     all       all

Posted: Sat Aug 13, 2005 8:11 pm
by feyd
try

Code: Select all

$alert = "SELECT id, email, first_name, last_name, lead_alert FROM members WHERE
(location LIKE '%".$country."%' OR industry LIKE '%".$industry."%')
OR (location='All' AND industry='All')";

Posted: Sat Aug 13, 2005 9:05 pm
by taldos
thanks, but that statement would not meet the required parameters as a user would be selected if either his location or industry matched. The requirements are for both to match. Think I got i licked thought. thanks

Cheers.

Posted: Sat Aug 13, 2005 9:14 pm
by feyd
:oops: oops.. I meant to change that first OR to an AND..