在 mysql 情况下使用 Where 子句

Using Where clause in case mysql

下面是我的查询,我正在正确获取相关状态值的总数。但我需要特定时间范围内的这些值。为了获得下面的总值是我的查询。

SELECT 
SUM(CASE 
     WHEN `status` = 'saved_for_review' THEN 1
     ELSE 0
   END) AS SavedCases,
SUM(CASE 
     WHEN STATUS = 'send_to_warehouse' THEN 1
     ELSE 0
   END) AS SendtoWareshouse,
SUM(CASE 
     WHEN STATUS = 'send_to_cep' THEN 1
     ELSE 0
   END) AS SendtoCEP,
   SUM(CASE 
     WHEN STATUS = 'Successful' THEN 1
     ELSE 0
   END) AS Closed 
FROM table_details

但我需要特定时间范围内的值,例如

where date(created_on) between '20160701' and '20160901'

如何将此 where 子句添加到上述查询中。任何帮助将不胜感激。

首先,只需编写 SUM(status='saved_for_review') SavedCases 即可,而且更简单、更快捷。至于WHERE子句,你只要在FROM子句后面加上即可。

SELECT 
    SUM(status = 'saved_for_review') SavedCases,
    SUM(status = 'send_to_warehouse') SendtoWarehouse,
    SUM(status = 'send_to_cep') SendtoCEP,
    SUM(status = 'Successful') Closed 
FROM table_details
WHERE DATE(created_on) BETWEEN '20160701' AND '20160901'