MySQL - 返回列的计数(),即使0结果为

时间:2018-05-22 23:59:24

标签: mysql

我有2个表,我正在尝试编写一个报表查询,在这里我可以计算具有给定状态的所有任务。我想要计算所有任务状态,即使没有具有该任务状态的任务(意味着它应返回0行而不是该状态的任何内容)。我尝试过以下方法:

     select t.TaskPriorityCode as priority,
count(tp.code) as count
   from task t
    LEFT JOIN priority tp on tp.code = t.TaskPriorityCode
       where t.companyId = 16
   and t.projectId = 4
    group by t.TaskPriorityCode

返回:

CRITICAL    4
HIGH    1
LOW 1

但是正如您所看到的,MEDIUM的任务状态不会出现,因为它没有记录。我希望此查询返回此信息:

CRITICAL    4
HIGH    1
LOW 1
MEDIUM 0

下面是一些SQL,可以为您提供我在开发中修改内容以及插入内容的快照。

CREATE TABLE `priority` (
  `code` varchar(20) NOT NULL,
  `display` varchar(255) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `metavalue` varchar(255) DEFAULT NULL,
  `createdAt` datetime NOT NULL,
  `updatedAt` datetime NOT NULL
) 

INSERT INTO `priority` VALUES ('CRITICAL','Critical','Critical',NULL,'2018-04-08 18:36:51','2018-04-08 18:36:51'),('HIGH','High','High',NULL,'2018-04-08 18:36:51','2018-04-08 18:36:51'),('LOW','Low','Low',NULL,'2018-04-08 18:36:51','2018-04-08 18:36:51'),('MEDIUM','Medium','Medium',NULL,'2018-04-08 18:36:51','2018-04-08 18:36:51');

CREATE TABLE `task` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `taskName` varchar(255) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `startDt` datetime NOT NULL,
  `finishDt` datetime DEFAULT NULL,
  `estimatedHrs` float DEFAULT NULL,
  `completedHrs` float DEFAULT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1',
  `repetitiveDays` varchar(255) DEFAULT NULL,
  `createdAt` datetime NOT NULL,
  `submittedDt` datetime DEFAULT NULL,
  `closedDt` datetime DEFAULT NULL,
  `updatedAt` datetime NOT NULL,
  `deletedAt` datetime DEFAULT NULL,
  `assignorId` int(11) DEFAULT NULL,
  `assigneeId` int(11) DEFAULT NULL,
  `projectId` int(11) NOT NULL,
  `TaskStatusCode` varchar(20) NOT NULL,
  `TaskPriorityCode` varchar(20) NOT NULL,
  `companyId` int(11) NOT NULL
)


INSERT INTO `task` VALUES (8,'My First Task  - Fix errors','My First Task  - Fix errors Description','2018-01-01 05:00:00','2018-01-01 05:00:00',100,50,1,'wed,sat,sun','2018-04-16 14:35:02','2018-04-16 15:06:56','2018-04-16 15:23:35','2018-05-13 23:43:56',NULL,13,13,4,'IN_PROGRESS','CRITICAL',16),(9,'Second Task - again fix errors','Second Task - again fix errors','2018-01-01 05:00:00','2018-05-01 04:00:00',50,10,1,'','2018-04-16 15:35:52',NULL,NULL,'2018-04-16 15:36:57',NULL,13,13,5,'IN_PROGRESS','MEDIUM',16),(10,'Third Task - again fix errors','Third Task - again fix errors','2018-04-01 04:00:00','2018-04-01 04:00:00',100,100,1,'','2018-04-16 15:36:36','2018-04-19 16:07:14','2018-04-19 16:07:36','2018-04-19 16:07:36',NULL,13,13,4,'CLOSED','LOW',16),(11,'Fourth Task - again fix errors','Third Task - again fix errors','2018-01-01 05:00:00','2018-05-01 04:00:00',200,0,1,'','2018-04-16 15:37:54',NULL,NULL,'2018-04-16 15:38:32',NULL,13,13,4,'ASSIGNED','HIGH',16),(12,'Fifth Task - again fix errors','Fifth Task - again fix errors','2018-01-01 05:00:00','2018-06-01 04:00:00',22,NULL,1,'','2018-04-16 15:39:49',NULL,NULL,'2018-04-16 15:39:49',NULL,13,13,4,'ASSIGNED','CRITICAL',16),(13,'Fix payment',NULL,'2018-05-04 04:00:00','2018-05-11 04:00:00',40,NULL,0,'','2018-05-04 14:50:31',NULL,NULL,'2018-05-04 14:53:34','2018-05-04 14:53:34',15,15,4,'ASSIGNED','CRITICAL',16),(14,'New','new','2018-05-01 04:00:00','0000-00-00 00:00:00',20,NULL,1,'','2018-05-13 23:44:50',NULL,NULL,'2018-05-13 23:44:50',NULL,13,13,4,'ASSIGNED','CRITICAL',16);

1 个答案:

答案 0 :(得分:2)

您确实需要先将priority带入查询,因为您知道您希望此表中包含所有行。然后将task加入其中,并在连接条件中包含所有条件,结果为:

select tp.code as priority,
       COALESCE(count(t.TaskPriorityCode), 0) as count
  from priority tp
  LEFT JOIN task t
    on tp.code = t.TaskPriorityCode AND
       t.companyId = 16 and
       t.projectId = 4
  group by t.TaskPriorityCode

SQLFiddle here

祝你好运。