MySQL 中具有相同 table 的多个 Select 不起作用
Multiple Select with same table in MySQL doesn't work
我的数据库的多个 selection 有问题。我需要做一个 select,return 是一种 table 的处理数据,需要按月份排序。为此,我使用了多个 select 的 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'
现在 return 计算我的 table 中所有行的总和,但我做不到 return 它按天分组,请帮助我!
结果一定是这样的:
Result that i need
我想你是说当我的查询都是子查询时,我如何按日期分组。我会将其更改为案例陈述。尝试这样的事情
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)
我的数据库的多个 selection 有问题。我需要做一个 select,return 是一种 table 的处理数据,需要按月份排序。为此,我使用了多个 select 的 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'
现在 return 计算我的 table 中所有行的总和,但我做不到 return 它按天分组,请帮助我!
结果一定是这样的:
Result that i need
我想你是说当我的查询都是子查询时,我如何按日期分组。我会将其更改为案例陈述。尝试这样的事情
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)