Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of instances of each foreign-key ID in a table?

Tags:

sql

mysql

Here's my simple SQL question...

I have two tables:

Books

------------------------------------------------------- | book_id | author | genre | price | publication_date | ------------------------------------------------------- 

Orders

------------------------------------ | order_id | customer_id | book_id | ------------------------------------ 

I'd like to create a query that returns:

-------------------------------------------------------------------------- | book_id | author | genre | price | publication_date | number_of_orders | -------------------------------------------------------------------------- 

In other words, return every column for ALL rows in the Books table, along with a calculated column named 'number_of_orders' that counts the number of times each book appears in the Orders table. (If a book does not occur in the orders table, the book should be listed in the result set, but "number_of_orders" should be zero.

So far, I've come up with this:

SELECT     books.book_id,     books.author,     books.genre,     books.price,     books.publication_date,     count(*) as number_of_orders from books left join orders on (books.book_id = orders.book_id) group by     books.book_id,     books.author,     books.genre,     books.price,     books.publication_date 

That's almost right, but not quite, because "number_of_orders" will be 1 even if a book is never listed in the Orders table. Moreover, given my lack of knowledge of SQL, I'm sure this query is very inefficient.

What's the right way to write this query? (For what it's worth, this needs to work on MySQL, so I can't use any other vendor-specific features).

Thanks in advance!

like image 968
mattstuehler Avatar asked Sep 15 '11 01:09

mattstuehler


People also ask

How many times can a foreign key be listed in a table?

A table can reference a maximum of 253 other tables and columns as foreign keys (outgoing references).

How do you count the number of entries in a table?

The SQL COUNT( ) function is used to return the number of rows in a table. It is used with the Select( ) statement.

Can you have multiple instances of the same foreign key in a table?

So in essence, you have have multiple foreign keys in a table to the same table, but they might mean something slightly different, but use the same reference data.


2 Answers

Your query is almost right and it's the right way to do that (and the most efficient)

SELECT books.*, count(orders.book_id) as number_of_orders         from books left join orders on (books.book_id = orders.book_id) group by     books.book_id 

COUNT(*) could include NULL values in the count because it counts all the rows, while COUNT(orders.book_id) does not because it ignores NULL values in the given field.

like image 172
Fabio Avatar answered Oct 20 '22 13:10

Fabio


SELECT b.book_id,     b.author,     b.genre,     b.price,     b.publication_date,     coalesce(oc.Count, 0) as number_of_orders from books b left join (     select book_id, count(*) as Count      from Order      group by book_id ) oc on (b.book_id = oc.book_id) 
like image 45
D'Arcy Rittich Avatar answered Oct 20 '22 14:10

D'Arcy Rittich