简体   繁体   中英

In MySQL, how to select empty records

I have the following MySQL Table Structure:

mysql> desc customers;
+---------------------+-------------+------+-----+---------+-------+
| Field               | Type        | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+-------+
| hash                | varchar(32) | NO   | PRI | NULL    |       |
| date_joined         | date        | NO   |     | NULL    |       |
| agent_code          | int(5)      | NO   | UNI |         |       |
+---------------------+-------------+------+-----+---------+-------+

mysql> desc persons;
+--------------------+-------------+------+-----+---------+-------+
| Field              | Type        | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+-------+
| agent_code         | int(5)      | NO   | UNI |         |       |
| team_id            | int(2)      | YES  |     | 0       |       |
| hash               | varchar(32) | NO   | PRI | NULL    |       |
+--------------------+-------------+------+-----+---------+-------+

mysql> desc teams;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(20) | YES  |     | NULL    |                |
| leader | varchar(32) | NO   | UNI | NULL    |                |
+--------+-------------+------+-----+---------+----------------+

And I'd wish to generate a report of sales by Team.

The SQL Query that I'm using is the following:

SELECT COUNT(`customers`.`agent_code) AS `customer_count`, `teams`.`name` 
FROM  `customers` 
JOIN  `persons` ON  `customers`.`agent_code` =  `persons`.`agent_code` 
JOIN  `teams` ON  `persons`.`team_id` =  `teams`.`id` 
GROUP BY  `teams`.`name`

And it shows the following information:

+----------------+--------+
| customer_count | name   |
+----------------+--------+
|              3 | Team 1 |
+----------------+--------+

However I'd like to see the "customer_count" of all the teams in the database, even if the customer_count is null (or zero) for a given team. I have 15 teams in my database, so I'd like to see something like:

+----------------+--------+
| customer_count | name   |
+----------------+--------+
|              3 | Team 1 |
|              0 | Team 2 |
|              0 | Team 3 |
|              0 | Team 4 |
+----------------+--------+

I have tried to execute some variants of the following Query, but I always get an error saying that the syntax of OUTER JOIN is incorrect, even though I've read the documentation, and it is correct.

 SELECT COUNT(  `customers`.`agent_code` ) AS  `customer_count` ,  `teams`.`name` 
 FROM  `customers` 
 LEFT JOIN  `persons` ON  `customers`.`agent_code` =  `persons`.`agent_code` 
 LEFT OUTER JOIN  `teams` ON  `persons`.`team_id` =  `teams`.`id` 
 GROUP BY  `teams`.`name` 

How can I alter my current query in order to display such result?

You have mistake get main table is person - I suggest your main table is team

SELECT COUNT(`customers`.`id`) AS  `customer_count` ,  `teams`.`name` 
 FROM  `teams`
 JOIN  `persons` ON `persons`.`team_id` = `teams`.`id`
 LEFT JOIN `customers`  ON  `customers`.`agent_code` =  `persons`.`agent_code` 
 GROUP BY  `teams`.`name`

Update: if you do have empty teams, than you need to set left join on persons

SELECT COUNT(`customers`.`id`) AS  `customer_count` ,  `teams`.`name` 
 FROM  `teams`
 LEFT JOIN  `persons` ON `persons`.`team_id` = `teams`.`id`
 LEFT JOIN `customers`  ON  `customers`.`agent_code` =  `persons`.`agent_code` 
 GROUP BY  `teams`.`name`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM