我目前有一个包含以下架构的表格:
CREATE TABLE `order_handling` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`oh_no` varchar(24) COLLATE utf8_unicode_ci DEFAULT NULL,
`date_request` datetime DEFAULT NULL,
`status` smallint(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我想执行这样的查询:
+-------------------------------------------------------------+
| MonthDone | OhProgress | OhHold | OhCancel | OhDone | Total |
+-------------------------------------------------------------+
| June 2016 | 2 | 1 | 1 | 3 | 7 |
| July 2016 | 6 | 1 | null | null | 7 |
+-------------------------------------------------------------+
我的查询是,
SELECT
DATE_FORMAT(`oh`.`date_request`, '%m-%Y') AS `IdMonth`,
(SELECT COUNT(*) FROM order_handling as oh1 WHERE `oh1`.`status` IN (1,2) GROUP BY IdMonth) As OhProgress,
(SELECT COUNT(*) FROM order_handling as oh2 WHERE `oh2`.`status` IN (3) GROUP BY IdMonth) As OhHold,
(SELECT COUNT(*) FROM order_handling as oh3 WHERE `oh3`.`status` IN (4) GROUP BY IdMonth) As OhCancel,
(SELECT COUNT(*) FROM order_handling as oh3 WHERE `oh3`.`status` IN (5) GROUP BY IdMonth) As OhDone,
(SELECT COUNT(*) FROM order_handling as oh5 WHERE `oh5`.`status` IN (1,2,3,4,5) GROUP BY IdMonth) As SumTotal
FROM `order_handling` `oh`
GROUP BY DATE_FORMAT(`oh`.`date_request`, '%M %Y')
ORDER BY `oh`.`date_request` ASC
预计不会产出。如果我用DATE_FORMAT(date_request, '%m-%Y')
更改SubQuery GROUP BY,如下所示:
SELECT
DATE_FORMAT(`oh`.`date_request`, '%m-%Y') AS `IdMonth`,
(SELECT COUNT(*) FROM order_handling as oh1 WHERE `oh1`.`status` IN (1,2) GROUP BY DATE_FORMAT(`oh1`.`date_request`, '%m-%Y')) As OhProgress,
(SELECT COUNT(*) FROM order_handling as oh2 WHERE `oh2`.`status` IN (3) GROUP BY DATE_FORMAT(`oh2`.`date_request`, '%m-%Y')) As OhHold,
(SELECT COUNT(*) FROM order_handling as oh3 WHERE `oh3`.`status` IN (4) GROUP BY DATE_FORMAT(`oh3`.`date_request`, '%m-%Y')) As OhCancel,
(SELECT COUNT(*) FROM order_handling as oh4 WHERE `oh4`.`status` IN (5) GROUP BY DATE_FORMAT(`oh4`.`date_request`, '%m-%Y')) As OhDone,
(SELECT COUNT(*) FROM order_handling as oh5 WHERE `oh5`.`status` IN (1,2,3,4,5) GROUP BY DATE_FORMAT(`oh5`.`date_request`, '%m-%Y')) As SumTotal
FROM `order_handling` `oh`
GROUP BY DATE_FORMAT(`oh`.`date_request`, '%M %Y')
ORDER BY `oh`.`date_request` ASC
它给了我错误:子查询返回超过1行 这是我的SQLFiddle
请帮忙, 谢谢
答案 0 :(得分:4)
不需要子查询
SELECT
DATE_FORMAT(`date_request`, '%m-%Y') AS `IdMonth`,
SUM(status IN (1,2) ) As OhProgress,
SUM(status IN (3) ) As OhHold,
SUM(status IN (4) ) As OhCancel,
SUM(status IN (5) ) As OhDone,
SUM(status IN (1,2,3,4,5)) As SumTotal
FROM `order_handling`
GROUP BY DATE_FORMAT(`date_request`, '%M %Y')
ORDER BY `date_request` ASC
SQLFiddle(由@Giorgos提供)
答案 1 :(得分:2)
试试这个:
SELECT
DATE_FORMAT(oh.`date_request`, '%m-%Y') AS `IdMonth`,
(SELECT COUNT(*)
FROM order_handling as oh1
WHERE oh1.`status` IN (1,2) AND
`IdMonth` = DATE_FORMAT(oh1.`date_request`, '%m-%Y')) As OhProgress,
(SELECT COUNT(*)
FROM order_handling as oh2
WHERE oh2.`status` IN (3) AND
`IdMonth` = DATE_FORMAT(oh2.`date_request`, '%m-%Y')) As OhHold,
(SELECT COUNT(*)
FROM order_handling as oh3
WHERE oh3.`status` IN (4) AND
`IdMonth` = DATE_FORMAT(oh3.`date_request`, '%m-%Y')) As OhCancel,
(SELECT COUNT(*)
FROM order_handling as oh4
WHERE oh4.`status` IN (5) AND
`IdMonth` = DATE_FORMAT(oh4.`date_request`, '%m-%Y')) As OhDone,
(SELECT COUNT(*)
FROM order_handling as oh5
WHERE oh5.`status` IN (1,2,3,4,5) AND
`IdMonth` = DATE_FORMAT(oh5.`date_request`, '%m-%Y')) As SumTotal
FROM `order_handling` oh
GROUP BY DATE_FORMAT(oh.`date_request`, '%M %Y')
ORDER BY oh.`date_request` ASC