To produce a count-per-author summary that includes even authors with no books in the book table, use a LEFT J : COUNT « Aggregate Functions « SQL / MySQL






To produce a count-per-author summary that includes even authors with no books in the book table, use a LEFT J

        
OIN
mysql>
mysql> CREATE TABLE author
    -> (
    ->     a_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # author ID
    ->     name VARCHAR(30) NOT NULL, # author name
    ->     PRIMARY KEY (a_id),
    ->     UNIQUE (name)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE book
    -> (
    ->     a_id INT UNSIGNED NOT NULL, # author ID
    ->     p_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # book ID
    ->     title VARCHAR(100) NOT NULL, # title of book
    ->     state VARCHAR(2) NOT NULL, # state where purchased
    ->     price INT UNSIGNED, # purchase price (dollars)
    ->     INDEX (a_id),
    ->     PRIMARY KEY (p_id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO author (name) VALUES
    ->  ('Tom'),
    ->  ('Monet'),
    ->  ('Jack'),
    ->  ('Picasso'),
    ->  ('Mary')
    -> ;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'Database', 'IN', 34 FROM author WHERE name = 'Tom';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'SQL', 'MI', 87 FROM author WHERE name = 'Tom';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'MySQL', 'KY', 48 FROM author WHERE name = 'Jack';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'XML', 'KY', 67    FROM author WHERE name = 'Jack';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'Java', 'IA', 33   FROM author WHERE name = 'Jack';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO book (a_id,title,state,price)
    ->  SELECT a_id, 'HTML', 'NE', 64   FROM author WHERE name = 'Mary';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT author.name AS painter, COUNT(book.a_id) AS count
    -> FROM author LEFT JOIN book ON author.a_id = book.a_id
    -> GROUP BY author.name;
+---------+-------+
| painter | count |
+---------+-------+
| Jack    |     3 |
| Mary    |     1 |
| Monet   |     0 |
| Picasso |     0 |
| Tom     |     2 |
+---------+-------+
5 rows in set (0.00 sec)

mysql>
mysql> drop table author;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table book;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.COUNT([DISTINCT] { | *})
2.Get the number of items for each type of wood
3.Get the number of woods for each type of item
4.Counting
5.How many times did drivers travel more than 200 miles in a day?
6.COUNT and GROUP BY
7.Count by group
8.COUNT(expr) doesn't count NULL values is useful when producing multiple counts from the same set of values.
9.The different forms of COUNT( ) can be very useful for counting missing values
10.Put the COUNT( ) expression in a HAVING clause instead.
11.Tracking Down Duplicates
12.Queries counting duplicate records have the following form
13.Counting and Identifying Duplicates
14.Count number of rows containing duplicated names
15.To see which names are duplicated in the cat_mailing table, use a summary query that displays the non-unique v
16.Eliminating Duplicates from a Query Result
17.Removing Duplicates of a Particular Row
18.Count duplicate records
19.How many days did Suzi drive?
20.How many messages were sent by each message sender
21.How many states did the United States consist of at the beginning of the 20th century?
22.Or to count weekend versus weekday trips
23.Count each value and see which one is most common
24.Generating Frequency Distributions
25.Relative frequency distributions
26.Counting Missing Values
27.Count them directly using SUM(ISNULL(score)).
28.Count the total number of rows
29.Categorizing Non-Categorical Data
30.Total number of the table vs unique values count