Sagewire Logo

Counting number of associated many-to-many items

5 Message(s) by 3 Author(s) originally posted in mysql discussion


From: herbasher Date:   Friday, October 26, 2007
Hello!

I have three table s, 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 help me out with this?
Thank you,
Rob


From: herbasher Date:   Friday, October 26, 2007
wrote in message:
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 help me out with this?



Is this hard? Possible? Expensive?Thanks


From: Kees Nuyt Date:   Friday, October 26, 2007
On Fri, 26 Oct 2007 14:29:26 -0700, herbasher
wrote in message:

wrote in message:
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 help me out with this?
Is this hard? Possible? Expensive?



No, but it's Friday ;)
I'm sure you will get some response later.
--
( Kees
)
c[_] The desire to become a politician should bar you
for life from ever becoming one. (Billy Connolly) (#232)


From: herbasher Date:   Saturday, October 27, 2007
Ok, I have received an answer on the mysql mailing list , here it is
for the record:

From: mysql@xxxxxxxxxxx
Subject: Re: Counting number of associated many-to-many items
Date: October 26, 2007 10:38:38 PM MDT
To: mysql@xxxxxxxxxxx!
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@xxxxxxxxxxx


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



Next Message: Copy data from MsSql database to a MySql database in batch mode.


Blogs related to Counting number of associated many-to-many items

ScissosShear_UltraShortBobBlone
In 1969, following a failed attempt by Lennon and McCartney to buy the company, James and Silver sold Northern Songs to British TV company Associated TeleVision (ATV), from which Lennon and McCartney received stock. ...

24 new messages in 13 topics - digest
to make use of my has_many association. While it does bring a little > Request knowledge into User, I think user.requests.find_active is > cleaner and makes more sense than Request.find_active(user). To me, ...

25 new messages in 19 topics - digest
Error: Unknown database 'shovell_development' plus a number of other 'from' lines. I created the shovell_development database earlier exactly as the book instructed & I've used the show command in mysql plus manually checking ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional