Jump to content

[SOLVED] left join weirdness - mysql count functions returning wrong!


theinfamousmielie

Recommended Posts

Hey all,

 

Curious question. This is the query i'm running:

 

SELECT
Client.Name,
Count(Campaign.CampaignID) AS CampaignCount,
Count(Content.ContentID) AS ContentCount,
Max(Content.UploadDT) AS LastUpload
FROM (Client LEFT JOIN Content ON Client.ClientID = Content.ClientID) LEFT JOIN Campaign ON Client.ClientID = Campaign.ClientID
GROUP BY Client.Name

 

And this is the result i'm getting:

 

+-----------------------+---------------+--------------+---------------------+
| Name                  | CampaignCount | ContentCount | LastUpload          |
+-----------------------+---------------+--------------+---------------------+
| Digiworks             |             0 |            0 | NULL                |
| John Smith Industries |             0 |            0 | NULL                |
| Pfizer                |             6 |            6 | 2008-06-16 17:26:56 |
| Playback              |             0 |            0 | NULL                |
+-----------------------+---------------+--------------+---------------------+

 

 

... the problem is: there are only 3 content items, and 2 campaigns ;) 3 * 2 = 6, but why it's returning both 6 for CampaignCount and ClientCount is beyond me. Any ideas? Is it a 'group by' problem?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.