-1

我的数据库的多项选择有问题。我需要做一个选择,它返回一种带有一些已处理数据的表,并且需要按月中的某天排序。为此,我使用了多重选择的 mysql 问题。这是我的代码:

SELECT 
    (SELECT COUNT( * ) FROM `table` WHERE `type`=1                                                                                AS 'Total',
    (SELECT COUNT( * ) FROM `table` WHERE `type`=1 and `status` = 0 and `status_cancel` = 0                                       AS 'Open',
    (SELECT COUNT( * ) FROM `table` WHERE `type`=1 and `status_cancel` = 1                                                        AS 'Cancel',
    (SELECT COUNT( * ) FROM `table` WHERE `type`=1 and `date_finish` is not null and `status_cancel` = 0                          AS 'Finish',
    (SELECT COUNT( * ) FROM `table` WHERE `type`=1 and `result` >= 0 and  `date_finish` is not null and `status_cancel` = 0       AS 'Win',
    (SELECT COUNT( * ) FROM `table` WHERE `type`=1 and `result` < 0 and `date_finish` is not null and `status_cancel` = 0         AS 'Loss'

现在它返回我表中所有行的总数,但我不能这样做返回它按天分组,请帮助我!

结果必须是这样的:

我需要的结果

4

1 回答 1

0

我想你是说当我的查询都相当于子查询时,我如何按日期分组。我会将其更改为案例陈述。尝试这样的事情

SELECT  DATE(date_finish) as `date`, count(*) as 'Total', 
        SUM(CASE WHEN `status` = 0 AND `status_cancel` = 0 THEN 1 ELSE 0 END) as 'Open',
        SUM(CASE WHEN `status_cancel` = 1 THEN 1 ELSE 0 END) as 'Cancel',
        SUM(CASE WHEN `date_finish` is not null and `status_cancel` = 0 THEN 1 ELSE 0 END) as 'Finish',
        SUM(CASE WHEN `result` >= 0 AND `date_finish` is not null AND `status_cancel` = 0 THEN 1 ELSE 0 END) as 'Win',
        SUM(CASE WHEN `result` < 0 AND `date_finish` is not null AND `status_cancel` = 0 THEN 1 ELSE 0 END) as 'Loss'
from `table` 
WHERE `type` = 1 
group by DATE(date_finish)

测试:http ://rextester.com/HCRIU11065

于 2017-08-18T20:46:56.327 回答