Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-17-2011, 02:02 AM   PM User | #1
elabuwa
New Coder

 
Join Date: Oct 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
elabuwa is an unknown quantity at this point
Query Returning Wrong Results

Hello guys,

Can some one shed some light on why the below query is returning the wrong results. :'( :'(

Code:
SELECT campaign_details.pid,campaign_summary.uid,campaign_summary.customer_id FROM campaign_summary,campaign_details WHERE campaign_summary.camp_id=campaign_details.camp_id AND campaign_summary.uid='1' OR campaign_summary.uid='11' AND campaign_summary.customer_id='205' AND campaign_details.pid='7' GROUP BY campaign_details.pid
I am explicitly telling stupid sql to return only where the campaign_details.pid matches with 7 and campaign_summary.customer_id matches with 205.
But I get the below result set.
pid----uid---customer_id
1------1-----65
2------1-----84
4------1-----81
5------1-----63
7------1-----65
13-----1-----63

Why is it returning PID's other than 7?
Why is it returning customer_id's other than 205?
There should be no results at all.

Can someone please shed some light?
Mysql server v5.0.51
elabuwa is offline   Reply With Quote
Old 11-17-2011, 04:17 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
You need to put () parentheses around your WHERE statements to group them into
what you want from your query.
sunfighter is offline   Reply With Quote
Old 11-17-2011, 05:08 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
In other words:
Code:
SELECT D.pid, S.uid, S.customer_id 
FROM campaign_summary AS S, campaign_details AS D 
WHERE S.camp_id=D.camp_id 
AND ( S.uid='1' OR S.uid='11' )
AND S.customer_id='205' 
AND D.pid='7' 
GROUP BY D.pid
Why do you put '...' around *numbers*????

And you can eliminate the need for the OR if you learn to use IN.

So this is better:
Code:
SELECT D.pid, S.uid, S.customer_id 
FROM campaign_summary AS S, campaign_details AS D 
WHERE S.camp_id=D.camp_id 
AND S.uid IN (1, 11)
AND S.customer_id = 205
AND D.pid = 7 
GROUP BY D.pid
Not sure why you think you need the GROUP BY, either, but it won't hurt.

Probably an ORDER BY would be more useful.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-17-2011, 05:10 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
By the by, the reason it didn't work as written is because AND has higher precedence than OR.

So you were *effectively* asking for this:
Code:
SELECT D.pid, S.uid, S.customer_id 
FROM campaign_summary AS S, campaign_details AS D 
WHERE ( S.camp_id=D.camp_id AND S.uid='1' )
OR ( S.uid='11' AND S.customer_id='205' AND D.pid='7' )
GROUP BY D.pid
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 AM.


Advertisement
Log in to turn off these ads.