PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > Counting number of associated many-to-many items
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Counting number of associated many-to-many items

Réponse
 
LinkBack Outils de la discussion
Vieux 27/10/2007, 04h27   #1
Robert MannI
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Counting number of associated many-to-many items

Hello!

I have three tables, mapping out a n:n relationship of authors and the
books they worked on:

table 1: authors (id, name)
table 2: authorships (author_id, book_id)
table 3: books (id, name, bestseller tinyint)

Here's two different queries I want to run:

1. Select each author, and how many books he has worked on.
2. Select each author, and how many bestseller books (bestseller = 1)
he has worked on.

Not exactly sure how to do this, can someone me out with this?

Thank you,
Rob
  Réponse avec citation
Vieux 27/10/2007, 05h38   #2
mysql@subtropolix.org
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Counting number of associated many-to-many items

Robert MannI wrote:
> Hello!
>
> I have three tables, mapping out a n:n relationship of authors and the
> books they worked on:
>
> table 1: authors (id, name)
> table 2: authorships (author_id, book_id)
> table 3: books (id, name, bestseller tinyint)
>
> Here's two different queries I want to run:
>
> 1. Select each author, and how many books he has worked on.


SELECT a.id, a.name, COUNT(b.id) AS oeuvre FROM authors AS a
LEFT JOIN authorships AS asp ON asp.author_id = a.id
LEFT JOIN books AS b ON asp.book_id = b.id
GROUP BY a.id;

This will also take into account books whose authorship is shared.

> 2. Select each author, and how many bestseller books (bestseller = 1)
> he has worked on.


SELECT a.id, a.name, COUNT(b.id) AS bestsellers FROM authors AS a
LEFT JOIN authorships AS asp ON asp.author_id = a.id
LEFT JOIN books AS b ON asp.book_id = b.id
WHERE b.bestseller = 1
GROUP BY a.id;

Only added the WHERE clause and changed the 3rd column name.

HTH
  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 12h58.


É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,07506 seconds with 10 queries