Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > Query returning more rows than it should.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-07-07, 11:16
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Query returning more rows than it should.

Well, at least to me it is..

Why.. oh why am I getting 3 rows returned in my result set when I should only be getting 2?

the active column should be eliminating one row but instead includes it. If I delete either the city or zip constraint, it works fine by only giving me the active users.

I want to be able to search on 2 columns from one textbox. It is just cleaner that way. That is the reason for the "OR".

Can someone please tell me what I am doing wrong please?

Code:
WHERE t1.active ='1' AND t4.city = 'mesa' OR t3.zip_Code = '85210'
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #2 (permalink)  
Old 11-07-07, 11:27
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,573
The AND binds more tightly than the OR does. What you need is:
Code:
WHERE t1.active ='1' AND (t4.city = 'mesa' OR t3.zip_Code = '85210')
-PatP
Reply With Quote
  #3 (permalink)  
Old 11-07-07, 11:39
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Holy cow Pat, it worked. Thanks.

I know that at one point I had it exactly in the same order as what you posted (minus the brackets) but it still returned 3 rows.

For fun, I removed the brackets and I again got 3 rows returned. Why do the brackets make it work correctly?

Thanks

Frank
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #4 (permalink)  
Old 11-07-07, 12:43
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 327
without them the query reads as follows:
Code:
WHERE t1.active ='1' AND t4.city = 'mesa' OR t3.zip_Code = '85210'

return any rows where active=1 and city ='mesa'
OR
give me any rows where zip_code='85210' irrespective of any other conditions.
Reply With Quote
  #5 (permalink)  
Old 11-07-07, 12:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
Quote:
Originally Posted by fjm1967
Why do the brackets make it work correctly?
because ANDs take precedence over ORs

it's the same as multiplication and addition

2 + 3 * 4 = 24, not 20

if you want (2 + 3) * 4, you have to use parentheses

__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #6 (permalink)  
Old 11-07-07, 13:45
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Quote:
Originally Posted by r937
because ANDs take precedence over ORs

it's the same as multiplication and addition

2 + 3 * 4 = 24, not 20

if you want (2 + 3) * 4, you have to use parentheses

Thanks Rudy.. I hate being such a nube.
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #7 (permalink)  
Old 11-07-07, 13:46
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Quote:
Originally Posted by guelphdad
without them the query reads as follows:
Code:
WHERE t1.active ='1' AND t4.city = 'mesa' OR t3.zip_Code = '85210'

return any rows where active=1 and city ='mesa'
OR
give me any rows where zip_code='85210' irrespective of any other conditions.
Thank you also for the explanation.. It makes clear sense to me now..
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #8 (permalink)  
Old 11-07-07, 13:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
pat's explanation should have been clear too...

"The AND binds more tightly than the OR does."

except you were probably thinking of bondage...

__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #9 (permalink)  
Old 11-07-07, 14:34
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Quote:
Originally Posted by r937
pat's explanation should have been clear too...

"The AND binds more tightly than the OR does."

except you were probably thinking of bondage...

lol. You mean he wasn't talking about bondage?
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On