2016-12-28 48 views
1

我下面的表格有:MySQL的情況下,地方「只適用於一定的價值

id | date_inserted | type  | route_id | route_type | km 
1 2016-01-05  Transport null  Normal  null 
2 2016-01-06  Transport null  Normal  50 
3 2016-01-07  Transport 1   Normal  null 
4 2016-04-02  Transport 2   Normal  null 
5 2016-05-03  Services  null  Normal  20 
6 2016-06-21  Transport null  Exceptional 35 

,我需要通過幾個月來檢索總的路線。這是通過執行實現:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     type IN ('Transport', 'Services') 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC 

的輸出是一樣的東西:

type  | total | month 
Transport 3  1 
Transport 1  2 
Services  1  5 
Transport 1  6 

,它工作正常。不過,現在有人問我申請了一些條件,只有當類型是Transport,條件如下:

  1. route_id不能爲null或
  2. route_type必須Exceptional
  3. route_typeNormalkm不等於零

所以,根據這個條件,這是我已經試過:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     type IN ('Transport', 'Services') AND 
     (CASE WHEN type = 'Transport' THEN 
      route_id IS NOT NULL OR 
      route_type = 'Exceptional' OR 
      (route_type = 'Normal' AND km != 0) 
     END) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC 

但我的輸出是:

type  | total | month 
Transport 2  1 
Transport 1  2 
Transport 1  6 

Services行缺少。

+1

添加一個'ELSE '返回True的'CASE'語句的條件...例如'CASE WHERE <...> ELSE 1 = 1 END' – abigperson

+0

您的情況下將返回「服務」爲空。在另一個'When'或'else'處理它 – GurV

+0

@PJSantoro爲什麼不把它作爲答案?分析和提出的解決方案都是正確的。 – jpw

回答

1

您需要添加一個else段來處理服務。如果你想的情況下是允許正常處理使用1 = 1

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
    type IN ('Transport', 'Services') AND 
    (CASE WHEN type = 'Transport' THEN 
     route_id IS NOT NULL OR 
     route_type = 'Exceptional' OR 
     (route_type = 'Normal' AND km != 0) 
    ELSE 
    1 = 1 
    END) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC 
1

正如其他人說,你需要一個ELSE添加到您的情況下,否則當行是「服務」,它會返回null,不顯示記錄。

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     type IN ('Transport', 'Services') AND 
     (CASE WHEN type = 'Transport' THEN 
      route_id IS NOT NULL OR 
      route_type = 'Exceptional' OR 
      (route_type = 'Normal' AND km != 0) 
      ELSE 1 = 1 
     END) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC; 

另一個版本,你可以使用(我喜歡)是:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     (type = 'Services' OR 
      (type = 'Transport' and 
        (route_id IS NOT NULL OR 
         route_type = 'Exceptional' OR 
         (route_type = 'Normal' AND km != 0) 
        ) 
     ) 
    ) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC; 

如果route_type只能是NormalExceptional您可以更改條件:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     (type = 'Services' OR 
      (type = 'Transport' and 
        (route_id IS NOT NULL OR 
        route_type = 'Exceptional' OR 
        km != 0 
        ) 
     ) 
    ) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC; 
相關問題