代码之家  ›  专栏  ›  技术社区  ›  user3408779

在mysql[closed]的情况下使用Where子句

  •  -1
  • user3408779  · 技术社区  · 8 年前

    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'
    

    1 回复  |  直到 8 年前
        1
  •  0
  •   jussius    8 年前

    首先,只是写作 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'