1

我有 2 个包含多个字段的表。

表格1:

+--------+---+---------------+                                                  
| month  | id| VERDICT_id    |                                                  
+--------+------+------------+                                                  
| 201307 | 1 |             1 |                                                  
| 201307 | 2 |             4 |                                                  
| 201307 | 3 |             2 |                                                  
| 201307 | 4 |             2 |                                                  
| 201307 | 5 |          NULL |                                                  
| 201307 | 6 |             1 |                                                  
+--------+------+------------+

就像每个新的“月”一样,对于每个唯一的“id”,“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 |
+--------+------------+----------+

这两个输出之间的区别是,如果一个月内不存在任何 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”?

4

3 回答 3

2

尝试这个

SELECT v.*, count(t.verdict_id) as cnt FROM 
(
      SELECT month, id as verdict_id 
      from 
          (SELECT DISTINCT month FROM Table1 where (month = 201307)) M , 
          verdictTable
) v
LEFT OUTER JOIN Table1 t ON v.verdict_id = t.verdict_id and v.month = t.month
GROUP BY v.verdict_id, v.month
于 2013-09-27T15:15:44.223 回答
0

尝试

select month,VERDICT_id, COUNT(*) as num FROM Table1 
where (month = 201307) AND (VERDICT_id BETWEEN 1 AND 4) 
GROUP BY month, VERDICT_id having num > 0;
于 2013-09-27T15:14:44.687 回答
0

当记录不存在时,您需要强制使用月份和 VERDICT_id 字段。(月份是用 DISTINCT LIST 强制的。VERDICT_id 是用无条件的 JOIN 强制到 Table2 的)

SELECT LIST.`month` as `month`, Table2.id AS VERDICT_id, COUNT(Detail.id)
FROM 
  (SELECT DISTINCT `month`
  FROM Table1
  WHERE Table1.`month` = 201307) AS LIST
JOIN Table2
LEFT JOIN Table1 AS Detail
  ON LIST.`month` = Detail.`month`
    AND Table2.id = Detail.VERDICT_id
GROUP BY `month`, VERDICT_id
ORDER BY `month`, VERDICT_id;
于 2013-09-27T15:20:25.943 回答