MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 28th, 2013, 08:51 PM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Posts: 1,666 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 15 h 23 m 16 sec
Reputation Power: 71
GROUP BY in LEFT OUTER JOIN table

Hello;

What is the corrcet approach? I wanna count the number of rows of the joined table (if any rows exist). But as it is aggregated function looks like I have to use GROUP BY the COUNTed row. The messes everything up. How can I achieve this?

For example count the number of photos of each event.

Code:
SELECT event.id, COUNT(event_photos.photo) AS photos 
FROM events LEFT OUTER JOIN event_photos ON events.id=event_photos.event_id
GROUP BY photos
Thanks

Reply With Quote
  #2  
Old January 28th, 2013, 09:06 PM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Posts: 1,666 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 15 h 23 m 16 sec
Reputation Power: 71
I came up with this but I am not very happy about it:
Code:
SELECT 
 Q_photos.C,
`sincity_events`.`id` AS EID, 
GROUP_CONCAT(sincity_events_celebrities.id) as CELEBS, 
GROUP_CONCAT(sincity_celebrities.name) as CELEB_NAME

FROM (`sincity_events`)

LEFT JOIN `sincity_events_celebrities`
 ON `sincity_events_celebrities`.`event_id` = `sincity_events`.`id`

LEFT JOIN `sincity_celebrities` ON `sincity_celebrities`.`id` = `sincity_events_celebrities`.`celebrity_id`

LEFT OUTER JOIN 
(SELECT event_id, COUNT(1) AS C FROM sincity_event_photos GROUP BY event_id) AS Q_photos
ON Q_photos.event_id =  sincity_events.id

GROUP BY `sincity_events`.`id`
ORDER BY `date` ASC 
Please advise

Reply With Quote
  #3  
Old January 29th, 2013, 05:02 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,438 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 13 h 52 m 18 sec
Reputation Power: 4141
Quote:
Originally Posted by zxcvbnm
For example count the number of photos of each event.
change this --
Code:
SELECT event.id, COUNT(event_photos.photo) AS photos 
FROM events LEFT OUTER JOIN event_photos ON events.id=event_photos.event_id
GROUP BY photos
to this --
Code:
SELECT event.id, COUNT(event_photos.photo) AS photos 
FROM events LEFT OUTER JOIN event_photos ON events.id=event_photos.event_id
GROUP BY event.id
rule of thumb: your GROUP BY clause should always equal your SELECT clause without the aggregates
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #4  
Old January 29th, 2013, 05:03 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,438 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 13 h 52 m 18 sec
Reputation Power: 4141
Quote:
Originally Posted by zxcvbnm
I came up with this but I am not very happy about it:
neither am i

you ditched the derived tables i coached you through in the last thread for what reason?

Reply With Quote
  #5  
Old January 29th, 2013, 07:26 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
As a professional teacher (not SQL), I've never yet met a pupil who I thought was beyond help, but I suppose such a hypothetical pupil must exist.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > GROUP BY in LEFT OUTER JOIN table

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap