To force each category to be displayed, use a reference table and a LEFT JOIN. : Left Join « Join « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Join » Left Join 
To force each category to be displayed, use a reference table and a LEFT JOIN.
     
mysql> CREATE TABLE testscore
    -> (
    ->  subject INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  age             INT UNSIGNED NOT NULL,
    ->  sex             ENUM('M','F'NOT NULL,
    ->  score   INT,
    ->  PRIMARY KEY (subject)
    -> );
Query OK, rows affected (0.00 sec)

mysql>
mysql> INSERT INTO testscore (age,sex,score)
    ->  VALUES
    ->  (5,'M',5),
    ->  (5,'M',4),
    ->  (5,'F',6),
    ->  (5,'F',7),
    ->  (6,'M',8),
    ->  (6,'M',9),
    ->  (6,'F',4),
    ->  (6,'F',6),
    ->  (7,'M',8),
    ->  (7,'M',6),
    ->  (7,'F',9),
    ->  (7,'F',7),
    ->  (8,'M',9),
    ->  (8,'M',6),
    ->  (8,'F',7),
    ->  (8,'F',10),
    ->  (9,'M',9),
    ->  (9,'M',7),
    ->  (9,'F',10),
    ->  (9,'F',9)
    -> ;
Query OK, 20 rows affected (0.00 sec)
Records: 20  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT subject, age, sex, score FROM testscore ORDER BY subject;
+---------+-----+-----+-------+
| subject | age | sex | score |
+---------+-----+-----+-------+
|       |   | M   |     |
|       |   | M   |     |
|       |   | F   |     |
|       |   | F   |     |
|       |   | M   |     |
|       |   | M   |     |
|       |   | F   |     |
|       |   | F   |     |
|       |   | M   |     |
|      10 |   | M   |     |
|      11 |   | F   |     |
|      12 |   | F   |     |
|      13 |   | M   |     |
|      14 |   | M   |     |
|      15 |   | F   |     |
|      16 |   | F   |    10 |
|      17 |   | M   |     |
|      18 |   | M   |     |
|      19 |   | F   |    10 |
|      20 |   | F   |     |
+---------+-----+-----+-------+
20 rows in set (0.00 sec)

mysql>
mysql> CREATE TABLE ref (score INT);
Query OK, rows affected (0.00 sec)

mysql> INSERT INTO ref (score)
    -> VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT ref.score, COUNT(testscore.scoreAS occurrences
    -> FROM ref LEFT JOIN testscore ON ref.score = testscore.score
    -> GROUP BY ref.score;
+-------+-------------+
| score | occurrences |
+-------+-------------+
|     |           |
|     |           |
|     |           |
|     |           |
|     |           |
|     |           |
|     |           |
|     |           |
|     |           |
|     |           |
|    10 |           |
+-------+-------------+
11 rows in set (0.00 sec)

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

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

   
    
    
    
    
  
Related examples in the same category
1.LEFT JOIN tables
2.'USING' command in LEFT JOIN
3.Two LEFT JOIN in select command
4.Two LEFT JOIN
5.Creating the table list with LEFT JOIN and then forming the linking connection with ON
6.A LEFT JOIN and a regular join
7.To list each author record, whether or not there are any book records for it, use a LEFT JOIN
8.Creating Left Joins
9.Replacing the ON clause with the USING clause for Left Join
10.Use a left join to link more than two tables.
11.Addition of the LEFT keyword to each join definition
12.Left Joins (Left Outer Joins)
13.Performing a LEFT JOIN on just the customer and sales tables.
14.Table order in a LEFT JOIN is important.
15.Left outer join syntax
16.Left outer join then order
17.Left outer join
18.Left outer join with using clause
19.Left outer join with subquery
20.Query from left outer join
21.To return all the sales reps who have not yet made a sale
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.