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 02-20-2013, 08:58 PM   PM User | #1
lilmousiee
New Coder

 
Join Date: Nov 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
lilmousiee is an unknown quantity at this point
COUNT() twice in one query showing weird results

Ok this is my query:

Code:
SELECT COUNT(d.dragon_id) as dragon_count, COUNT(pm.id) as pm_count
FROM user_creature d
LEFT JOIN private_message pm ON pm.to_user = d.user_id
WHERE d.user_id = 4
LIMIT 1
It's giving me 10 as dragon_count and 10 as pm_count when in reality, it should be 1 as dragon_count and 10 for pm_count.
Why does dragon_count go up to 10 when I add on the private_message table and do the COUNT() for that. Without the private_message table included and in the SELECT, it pulls 1 just fine..
lilmousiee is offline   Reply With Quote
Old 02-20-2013, 09:08 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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
Nope, you are wrong.

Let's demonstrate why:
Code:
SELECT d.dragon_id, pm.id AS message_id
FROM user_creature d
LEFT JOIN private_message pm ON pm.to_user = d.user_id
WHERE d.user_id = 4
Don't you get 10 rows from that?

So aren't there 10 rows with a dragon_id? Yes, they are all the same id. But that's irrelevant, to SQL.

So of course when you then do COUNT(dragon_id) you get 10.

*TRIVIAL* fix:
Code:
SELECT COUNT(DISTINCT d.dragon_id) as dragon_count, COUNT(pm.id) as pm_count
FROM user_creature d
LEFT JOIN private_message pm ON pm.to_user = d.user_id
WHERE d.user_id = 4
LIMIT 1
Hmm??
__________________
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 02-20-2013, 09:09 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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 way, your LIMIT 1 is a waste of code. That query will never return more than one row, as it is written.
__________________
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 02-20-2013, 09:14 PM   PM User | #4
lilmousiee
New Coder

 
Join Date: Nov 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
lilmousiee is an unknown quantity at this point
Wow.. you're right. I'm so confused.

The LIMIT 1 is a faster query. A lot of developers have notified me that I should be including LIMIT 1 in my queries.. Now I shouldn't?
lilmousiee is offline   Reply With Quote
Old 02-20-2013, 09:43 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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
Try it both with and without the LIMIT 1 and see if your results are any different.

If they are--with that query written as is--I owe you a latte.

You only use LIMIT when your query is going to return more records than you want.

And in any case, a LIMIT used *WITHOUT* and ORDER BY clause is worse than useless.
__________________
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:01 PM.


Advertisement
Log in to turn off these ads.