簡體   English   中英

在MySQL中,如何選擇空記錄

[英]In MySQL, how to select empty records

我有以下MySQL表結構:

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    |                |
+--------+-------------+------+-----+---------+----------------+

我希望生成Team的銷售報告。

我正在使用的SQL查詢如下:

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`

它顯示以下信息:

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

但是,我想查看數據庫中所有團隊的“ customer_count”,即使給定團隊的customer_count為null(或零)也是如此。 我的數據庫中有15個團隊,所以我希望看到以下內容:

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

我試圖執行以下查詢的某些變體,但是即使閱讀了文檔,也是正確的,但我總是收到一條錯誤消息,指出OUTER JOIN的語法不正確。

 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` 

如何更改當前查詢以顯示此類結果?

您有錯誤獲取主表是人-我建議您的主表是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`

更新:如果您確實有空團隊,則需要在人員上設置左聯接

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`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM