Print Page | Close Window

SQL query problem - WHERE / OR / AND

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=2899
Printed Date: 29 March 2026 at 12:01pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: SQL query problem - WHERE / OR / AND
Posted By: pedalcars
Subject: SQL query problem - WHERE / OR / AND
Date Posted: 21 May 2003 at 3:31am
I've hit a SQL query problem (Access database).

In a table, entries can be one of about half a dozen different types; additionally (regardless of type) there is a true/false entry.

I want to bring out entries of three types only, but ONLY those for which the true/false is also true.

Currently I have a statement in the form:

strSQL = "SELECT tblTable.* FROM tblTable WHERE tblTable.Type = 'type1' OR tblTable.Type = 'type2' OR tblTable.Type = 'type3' AND tblTable.TrueFalse = True;"

Unfortunately, this seems to bring back ALL 'type1' records, ALL 'type2' records, and 'type3' records which are also "True"; in psuedo-mathematical notation it's doing:

type1 OR type2 OR (type3 AND True)

When I want:

(type1 OR type2 OR type3) AND True

i.e., I want it to find all type1, 2 and 3 records which are True; it's only applying the True/False criteria to the last selected type. I need to force the statement to consider each "OR" first, and then consider the "AND" section.

Help!



PS. I've tried putting the True/False criteria first, then AND...type1 OR...type2 OR type3, it makes no difference beyond doing "(True AND Type1) OR type2 OR type3"

PPS. I don't really want to duplicate the "AND" section after each or the Type selection sections, mainly because it looks ugly and there must be a neater way!

Ta

-------------
http://www.pedalcars.info/ - www.pedalcars.info

The most fun on four wheels




Replies:
Posted By: MorningZ
Date Posted: 21 May 2003 at 6:21am

When I want:

(type1 OR type2 OR type3) AND True

 

So use that notation.. stuff inside ()'s get evaluated first



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: pedalcars
Date Posted: 21 May 2003 at 6:29am
Hey cool, something else I never knew / hadn't read about ASP!

So simple when you know how...

Thanks MorningZ, yet another of my ASP problems you've solved!

-------------
http://www.pedalcars.info/ - www.pedalcars.info

The most fun on four wheels



Posted By: Bunce
Date Posted: 21 May 2003 at 6:42am

There's also the IN() keyword:

WHERE tblTable.Type IN ('Type1','Type2','Type3') 

Cheers,
Andrew 

 



-------------
There have been many, many posts made throughout the world...
This was one of them.


Posted By: pedalcars
Date Posted: 22 May 2003 at 5:43am
Thanks Bunce - even neater!



-------------
http://www.pedalcars.info/ - www.pedalcars.info

The most fun on four wheels




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net