Unix Technical Forum

Amount of records in table and the id

This is a discussion on Amount of records in table and the id within the MySQL forums, part of the Database Server Software category; --> Hi, I have two tables, event and photo. Event holds all the information for an event whilst photo holds ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:30 AM
Iain Adams
 
Posts: n/a
Default Amount of records in table and the id

Hi,

I have two tables, event and photo. Event holds all the information
for an event whilst photo holds all photos uploaded for that event.

Now I have a problem. I want to get a list of all the event.ids but I
want them ordered by the amount of photos uploaded in the other table.
The trouble here is that some events do not have photos uploaded and
though should be bottom but still be there. I have tried over and
over again but I think I have got my selfed confused. If anyone has
any ideas I would be very grateful.

Thanks

Iain
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:30 AM
Iain Adams
 
Posts: n/a
Default Re: Amount of records in table and the id

Sorry, I figured it out in the end. My sql looks like this:

SELECT * FROM event LEFT JOIN (SELECT count(*), event_id FROM photos
GROUP BY event_id ORDER BY count(*) DESC) As a ON event.id =
a.event_id;


On Dec 18, 2:21 pm, Iain Adams <aca04...@shef.ac.uk> wrote:
> Hi,
>
> I have two tables, event and photo. Event holds all the information
> for an event whilst photo holds all photos uploaded for that event.
>
> Now I have a problem. I want to get a list of all the event.ids but I
> want them ordered by the amount of photos uploaded in the other table.
> The trouble here is that some events do not have photos uploaded and
> though should be bottom but still be there. I have tried over and
> over again but I think I have got my selfed confused. If anyone has
> any ideas I would be very grateful.
>
> Thanks
>
> Iain


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:30 AM
strawberry
 
Posts: n/a
Default Re: Amount of records in table and the id

On Dec 18, 11:00 pm, Iain Adams <aca04...@shef.ac.uk> wrote:
> Sorry, I figured it out in the end. My sql looks like this:
>
> SELECT * FROM event LEFT JOIN (SELECT count(*), event_id FROM photos
> GROUP BY event_id ORDER BY count(*) DESC) As a ON event.id =
> a.event_id;
>
> On Dec 18, 2:21 pm, Iain Adams <aca04...@shef.ac.uk> wrote:
>
> > Hi,

>
> > I have two tables, event and photo. Event holds all the information
> > for an event whilst photo holds all photos uploaded for that event.

>
> > Now I have a problem. I want to get a list of all the event.ids but I
> > want them ordered by the amount of photos uploaded in the other table.
> > The trouble here is that some events do not have photos uploaded and
> > though should be bottom but still be there. I have tried over and
> > over again but I think I have got my selfed confused. If anyone has
> > any ideas I would be very grateful.

>
> > Thanks

>
> > Iain


I think you've overcomplicated it...

SELECT e.*,COUNT(p.event_id) qty FROM event e
LEFT JOIN photos p ON p.event_id = e.event_id
GROUP BY p.event_id
ORDER BY qty DESC

Also IMO your table naming convention is a little inconsistent.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:50 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com