473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL Query using 'OR' Causing problems

markmcgookin
648 Expert 512MB
Hi Folks,

I am running a simple query using VB (This isnt a VB Question, dont worry!) on SQL Server Compact. I have the query below being created, and then added to if a flower location doesn't equal nothing (if it is nothing, its returning all the values) now, when this query runs and I select a flower location, i.e. L3 I want it to return all values where the flower location is equal to L3 and those where it is equal to L5.

Expand|Select|Wrap|Line Numbers
  1. statement = "SELECT * FROM tblDetails WHERE Dummy LIKE 'x'"
  2.  
  3. If flowerLocation <> "" Then
  4.                 statement = statement + " AND Location_ID LIKE '" + flowerLocation + "'"  OR Location_ID LIKE 'L5'"
  5.             End If
  6.  
But later on in my code, I have stuff like this
Expand|Select|Wrap|Line Numbers
  1. If flowerFamily <> "" Then
  2.                 statement = statement + " AND Family_ID LIKE '" + flowerFamily + "'"
  3.             End If
  4.  
  5.             If flowerHeight <> "" Then
  6.                 statement = statement + " AND Height LIKE '" + flowerHeight + "'"
  7.             End If
  8.  
Which is essentially adding to the statements,

so a possible statement could be :

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblDetails WHERE Dummy LIKE 'x'
  2. AND Location_ID LIKE 'L3' OR Location_ID LIKE 'L5'
  3. AND Family_ID LIKE 'F2'
  4.  
But.... my problem lies here, the query for some reason is always returning the two values that match the location even if they dont match the Family_ID

Are there issues about using an OR then an AND statement? its like the AND statement isn't "filtering" out the results returned by the 1st bit of the query.

If I take out the bit of code adding the OR Location_ID LIKE 'L5' on the code works fine, and only returns those which meet all your criteria, but it's the OR thats causing trouble, but i want to return all the values that match the location selected and those with L5, but not other locations. (NB The user can not select L5)

This is code for a search function, so the SELECT * is returning all the values in the DB and the ANDs are filtering out the stuff I dont want if the user selects something.

Hope thats clear, pls PM me if I haven't explained it well enough,

Thanks very much!!!

Mark
Apr 9 '07 #1
6 3023
iburyak
1,017 Expert 512MB
1. In VB concatenation sign is & and not +

I am not sure how this line works:

[PHP]statement = statement + " AND Family_ID LIKE '" + flowerFamily + "'"[/PHP]

I thought it should be:

[PHP]statement = statement & " AND Family_ID LIKE '" + flowerFamily + "'"[/PHP]

2. Query should have ( ) around or condition like this:

[PHP]SELECT * FROM tblDetails WHERE Dummy LIKE 'x'
AND (Location_ID LIKE 'L3' OR Location_ID LIKE 'L5')
AND Family_ID LIKE 'F2'[/PHP]

so change your statement to this:

[PHP]If flowerLocation <> "" Then
statement = statement & " AND (Location_ID LIKE '" + flowerLocation + "'" OR Location_ID LIKE 'L5')"
End If[/PHP]

Hope it helps.
Good Luck.
Apr 9 '07 #2
Mark, you just need some parentheses to group your locations. Whenever you mix ANDs and ORs you'll need to structure them a bit.

You had this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblDetails WHERE Dummy LIKE 'x'
  2. AND Location_ID LIKE 'L3' OR Location_ID LIKE 'L5'
  3. AND Family_ID LIKE 'F2'
  4.  
But you want this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM tblDetails 
  3. WHERE Dummy LIKE 'x'
  4. AND ( Location_ID LIKE 'L3' OR Location_ID LIKE 'L5' )
  5. AND Family_ID LIKE 'F2'
  6.  
Apr 14 '07 #3
markmcgookin
648 Expert 512MB
Mark, you just need some parentheses to group your locations. Whenever you mix ANDs and ORs you'll need to structure them a bit.

You had this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblDetails WHERE Dummy LIKE 'x'
  2. AND Location_ID LIKE 'L3' OR Location_ID LIKE 'L5'
  3. AND Family_ID LIKE 'F2'
  4.  
But you want this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM tblDetails 
  3. WHERE Dummy LIKE 'x'
  4. AND ( Location_ID LIKE 'L3' OR Location_ID LIKE 'L5' )
  5. AND Family_ID LIKE 'F2'
  6.  
Thanks for the replies folks,

I have had many AND statements in one query, and never used () before, the code I have generates a query with up to 9 ANDs and it works fine. I found a last minute workaround for the code that elimenated the need for the OR statement though. But i will go back and try with the () just for peace of mind! hehe. thanks for the help!

NB " + " works just the same as " & " in VB
Apr 14 '07 #4
Hi Mark, well it doesn't matter how many ANDs you use, it's the fact that you added an OR. :-) Throwing an OR into your boolean statement will usually require you to do some grouping to get the right behavior.

Here's my quick attempt at an pseudo code example. The first two statements define a desired outcome. Then I give a correct and incorrect way of combining them.
Expand|Select|Wrap|Line Numbers
  1. If  A=1 AND B=2 Then print "got it"
  2. If A=3 AND B=4 Then print "got it"
Taking the two statements above, you could produce the following:
Expand|Select|Wrap|Line Numbers
  1. If  (A=1 AND B=2) OR (A=3 AND B=4) Then print "got it"
Dropping the parentheses, to simply this:
Expand|Select|Wrap|Line Numbers
  1. ...A=1 AND B=2 OR A=3 AND B=4  ...
causes the statement to be interpreted as:
Expand|Select|Wrap|Line Numbers
  1. ...(A=1 AND B=2 AND B=4) OR A=3  ...
which doesn't represent the original rules.
Apr 16 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
NB " + " works just the same as " & " in VB
Be careful this is not exactly true and you will run into problems using " + " for concatenation particularly when numbers are involved. You should always use " & "
Apr 17 '07 #6
markmcgookin
648 Expert 512MB
Hi Mark, well it doesn't matter how many ANDs you use, it's the fact that you added an OR. :-) Throwing an OR into your boolean statement will usually require you to do some grouping to get the right behavior.

Here's my quick attempt at an pseudo code example. The first two statements define a desired outcome. Then I give a correct and incorrect way of combining them.
Expand|Select|Wrap|Line Numbers
  1. If  A=1 AND B=2 Then print "got it"
  2. If A=3 AND B=4 Then print "got it"
Taking the two statements above, you could produce the following:
Expand|Select|Wrap|Line Numbers
  1. If  (A=1 AND B=2) OR (A=3 AND B=4) Then print "got it"
Dropping the parentheses, to simply this:
Expand|Select|Wrap|Line Numbers
  1. ...A=1 AND B=2 OR A=3 AND B=4  ...
causes the statement to be interpreted as:
Expand|Select|Wrap|Line Numbers
  1. ...(A=1 AND B=2 AND B=4) OR A=3  ...
which doesn't represent the original rules.
Aaaah, very well put. I never thought of it like that, thanks very much! Very helpful reply. I actually changed the application as a workaround for this, but that is an excellent way of putting it. If I were to go back now, I could do it like that! cheers!
Apr 22 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting...
2
by: Stewart Allen | last post by:
Hi There I have a function that does some calculations depending on the 2 date arguments passed into it: Function GetPeriods(dteStart As Date, dteEnd As Date) As Integer The function has 2...
6
by: Nicolae Fieraru | last post by:
Hi All, I was trying to update a field in a table, based on the results from a query. The table to be updated is tblCustomers and the query is qrySelect. This query has two parameters, provided...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
10
by: B Moor | last post by:
Host PC: Win SBS 2003 with 2.6 GHz processor and 1GB RAM SQL Server 2000 sp3 (or latest).MS Office 2k3 I have the access 2k3 front end running on server (this may get changed) and all was well...
5
by: emanuel.levy | last post by:
I have a table called tbl_employers. One of the fields is start_date. I'm trying to make a query that will show all entries where todays date is 275 days and 305 days after the start date. Any...
3
by: juliehg | last post by:
Hi, I'm currently using MS Access 2000 to keep all my actors measurements, resumes and personal details on a database. To upload their details onto our website, I have built an event so when I...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.