我有兩個包含多個字段的表。MySQL查詢COUNT並從另一個表中加入列
表1:
+--------+---+---------------+
| month | id| VERDICT_id |
+--------+------+------------+
| 201307 | 1 | 1 |
| 201307 | 2 | 4 |
| 201307 | 3 | 2 |
| 201307 | 4 | 2 |
| 201307 | 5 | NULL |
| 201307 | 6 | 1 |
+--------+------+------------+
像這樣每一個新的「月」,對於每一個獨特的「身份證」的「VERDICT_ID」根據表2中的值設置。
表2:
+----+----------------------------------+
| id | title |
+----+----------------------------------+
| 1 | Passed |
| 2 | Failed (Component Fault) |
| 3 | Failed (User Fault) |
| 4 | Failed (Hardware Issue) |
+----+----------------------------------+
我做一個查詢這給下面的輸出
實際輸出:
+--------+------------+----------+
| month | VERDICT_id | COUNT(*) |
+--------+------------+----------+
| 201307 | 1 | 2 |
| 201307 | 2 | 2 |
| 201307 | 4 | 1 |
+--------+------------+----------+
我要的是,
+--------+------------+----------+
| month | VERDICT_id | COUNT(*) |
+--------+------------+----------+
| 201307 | 1 | 2 |
| 201307 | 2 | 2 |
| 201307 | 3 | 0 |
| 201307 | 4 | 1 |
+--------+------------+----------+
的區別該se 2輸出是,如果任何VERDICT_id在一個月內不存在,那麼我想打印VERDICT_id和COUNT爲'0'。
但用下面的查詢是不可能的。
select month,VERDICT_id, COUNT(*) FROM Table1
where (month = 201307) AND (VERDICT_id BETWEEN 1 AND 4) GROUP BY month, VERDICT_id;
問:是否可以使查詢打印不存在的VERDICT_id和COUNT爲'0'?
您需要做一個左連接|可能的重複[如何顯示在mySQL組中按零查詢計數行?](http://stackoverflow.com/questions/6602343/how-to-show-rows-with-zero-counts-in-mysql-group - 查詢) – Paulina