Forum Moderators: open

Message Too Old, No Replies

Multiple OR AND in SELECT

         

switchjohnny

3:37 am on Sep 30, 2007 (gmt 0)

10+ Year Member



I'm sure this has been covered many times on the forum, but after searching a few times and reading I couldn't find what I was looking for.

I'm trying to do this:

$query ="SELECT * FROM $sn WHERE name LIKE $var OR category LIKE $var AND zip = $zipcode ORDER BY name";

When I run this query, I get all names like $var from $sn. I don't get the category names like $var or the names only with $zip.

If I remove the AND zip = $zipcode, and run the query I get all names and categories like $var.

I want to select all names and categories like $var with the zipcode $zipcode.

Any ideas?

aspdaddy

5:42 pm on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should do it.

SELECT *
FROM $sn
WHERE (((name LIKE $var) OR (category LIKE $var)) AND (zip = $zipcode))
ORDER BY name

switchjohnny

7:00 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



That works,
Thanks.
I was at the bar last night and ran into a sql developer and he told me the same thing. I fixed it late last night, however, I have another problem.

I want to add on another piece of filtering

$query ="SELECT * FROM $sn WHERE (((name LIKE \"%$trimmed%\") OR (category LIKE \"%$trimmed%\")) AND ((city LIKE \"%$czt%\") OR (zip = $czt))) ORDER BY name";

I have 2 input fields where a user can type in a name or category, and a city or zip.

the above query doesn't work. It works if I put in a name or category with zip, but not a name or category with city.

switchjohnny

1:06 am on Oct 1, 2007 (gmt 0)

10+ Year Member



$query = "SELECT * FROM $sn WHERE ((name LIKE '%$trimmed%' OR category LIKE '%$trimmed%') AND (city LIKE '%$cz%' OR zip LIKE '$cz')) ORDER BY name";

this did it. changing the order of city and zip. don't know what that matter, but it's working