PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Amount of records in table and the id
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Amount of records in table and the id

Réponse
 
LinkBack Outils de la discussion
Vieux 18/12/2007, 22h21   #1
Iain Adams
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 18/12/2007, 23h00   #2
Iain Adams
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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


  Réponse avec citation
Vieux 18/12/2007, 23h38   #3
strawberry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h30.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11076 seconds with 11 queries