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 > 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 26/10/2007, 19h06   #1
herbasher
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 26/10/2007, 23h29   #2
herbasher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Counting number of associated many-to-many items

On Oct 26, 11:06 am, herbasher <rob...@gmail.com> 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.
> 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?


Is this hard? Possible? Expensive?


Thanks

  Réponse avec citation
Vieux 27/10/2007, 00h52   #3
Kees Nuyt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Counting number of associated many-to-many items

On Fri, 26 Oct 2007 14:29:26 -0700, herbasher
<robmnl@gmail.com> wrote:

>On Oct 26, 11:06 am, herbasher <rob...@gmail.com> 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.
>> 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?

>
>Is this hard? Possible? Expensive?


No, but it's Friday
I'm sure you'll get some response later.
--
( Kees
)
c[_] The desire to become a politician should bar you
for life from ever becoming one. (Billy Connolly) (#232)
  Réponse avec citation
Vieux 27/10/2007, 07h39   #4
herbasher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Counting number of associated many-to-many items

Ok, I have received an answer on the mysql mailing list, here it is
for the record:

From: mysql@subtropolix.org
Subject: Re: Counting number of associated many-to-many items
Date: October 26, 2007 10:38:38 PM MDT
To: mysql@lists.mysql.com


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

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=robmnl@gmail.com

  Réponse avec citation
Vieux 27/10/2007, 19h47   #5
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Counting number of associated many-to-many items

herbasher wrote:
> Ok, I have received an answer on the mysql mailing list, here it is
> for the record:
>
> From: mysql@subtropolix.org
> Subject: Re: Counting number of associated many-to-many items
> Date: October 26, 2007 10:38:38 PM MDT
> To: mysql@lists.mysql.com
>
>
> 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


Hmmm, in 1. the books table adds nothing to the query.

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
GROUP BY asp.book_id ;

Should do just as well.


  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 09h27.


É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,13841 seconds with 13 queries