If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
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, 10:16
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
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'
Reply With Quote
  #2 (permalink)  
Old 11-07-07, 10:27
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
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, 10:39
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
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
Reply With Quote
  #4 (permalink)  
Old 11-07-07, 11:43
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
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, 11:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 11-07-07, 12:45
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
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.
Reply With Quote
  #7 (permalink)  
Old 11-07-07, 12:46
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
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..
Reply With Quote
  #8 (permalink)  
Old 11-07-07, 12:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 11-07-07, 13:34
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
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?
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

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