2013-04-01 95 views
4

我想在MySQL中建立一個查詢,其中我有一個數值庫存水平的東西,並取決於什麼樣的股票水平,我希望它返回另一個值將是一個顏色代碼。MySQL案例/如果/然後

E.g.如果庫存水平爲< 0,則庫存水平爲0至1000之間爲空然後爲紅色,如果庫存水平在1000至5000之間則爲黃色,如果庫存水平在5000至10000之間則爲綠色,如果庫存水平> 10000,則爲綠色。

所以這裏是我的例子。我有一個啤酒表,它包含關於啤酒和保存股票價值的BeerStock表的信息。

SELECT Beer.Beer, Beer.Brewery, Beer.Style, Beer.ABV, Beer.Hops, Beer.SRM, 
Sum(BeerStock.Quantity) 
FROM Beer, BeerStock 
Where Beer.Beer = BeerStock.Beer 

這會給我這樣的事情:

Beer1 Brewery1 Style1 5%, 3, 10, 1238 

而且我想

Beer1 Brewery1 Style1 5%, 3, 10, 1238 YELLOW 

我似乎無法在所有的數字了這一點,我應該使用一個CASE或者我可以使用IF/THEN類型的東西?

我的SQL技能很生疏,通常我使用Access和亂七八糟的自動生成的SQL而不是從頭開始寫...我可以應付MySQL的基礎知識,但不是這種東西!

任何意見/讚賞指針....

感謝

安迪

+0

是一個CASE語句將起作用。也EMPTY = NULL和顏色可能會是十六進制值,如果你將在一些HTML輸出中使用它們。 – Randy

回答

8

我瘦你的意思是,如果stock > 1000然後另一種顏色not green

SELECT Beer.Beer, 
     Beer.Brewery, 
     Beer.Style, 
     Beer.ABV, 
     Beer.Hops, 
     Beer.SRM, 
     Sum(BeerStock.Quantity) totalQuantity, 
     CASE WHEN Sum(BeerStock.Quantity) < 0 THEN '' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 0 AND 999 THEN 'Red' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 1000 AND 4999 THEN 'Yellow' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 5000 AND 9999 THEN 'GREEN' 
      WHEN Sum(BeerStock.Quantity) >= 10000 THEN 'Another Color' 
     END 
FROM Beer 
     INNER JOIN BeerStock 
      ON Beer.Beer = BeerStock.Beer 
-- Where ..other conditions.. 

,但如果你真的這樣想,

SELECT Beer.Beer, 
     Beer.Brewery, 
     Beer.Style, 
     Beer.ABV, 
     Beer.Hops, 
     Beer.SRM, 
     Sum(BeerStock.Quantity) totalQuantity, 
     CASE WHEN Sum(BeerStock.Quantity) < 0 THEN '' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 0 AND 999 THEN 'Red' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 1000 AND 4999 THEN 'Yellow' 
      WHEN Sum(BeerStock.Quantity) >= 5000 THEN 'GREEN' 
     END 
FROM Beer 
     INNER JOIN BeerStock 
      ON Beer.Beer = BeerStock.Beer 
-- Where ..other conditions.. 

一件事,你還需要使用GROUP BY條款,否則你將回到只有一個,即使你有不同的記錄記錄,

SELECT Beer.Beer, 
     Beer.Brewery, 
     Beer.Style, 
     Beer.ABV, 
     Beer.Hops, 
     Beer.SRM, 
     Sum(BeerStock.Quantity) totalQuantity, 
     CASE WHEN Sum(BeerStock.Quantity) < 0 THEN '' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 0 AND 999 THEN 'Red' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 1000 AND 4999 THEN 'Yellow' 
      WHEN Sum(BeerStock.Quantity) BETWEEN 5000 AND 9999 THEN 'GREEN' 
      WHEN Sum(BeerStock.Quantity) >= 10000 THEN 'Another Color' 
     END 
FROM Beer 
     INNER JOIN BeerStock 
      ON Beer.Beer = BeerStock.Beer 
-- Where ..other conditions.. 
GROUP BY Beer.Beer, 
     Beer.Brewery, 
     Beer.Style, 
     Beer.ABV, 
     Beer.Hops, 
     Beer.SRM 
+1

非常完美,非常感謝! – user2232373

+0

不客氣':D' –

0

像這樣的東西應該工作使用CASE stateme NT:

SELECT Beer, Brewery, Style, ABV, SRM, SummedQty, 
    CASE 
     WHEN SummedQty >=0 AND SummedQty <= 1000 THEN ' RED' 
     WHEN SummedQty >1000 AND SummedQty <= 5000 THEN ' YELLOW' 
     WHEN SummedQty >5000 THEN ' GREEN' 
     ELSE '' 
    END yourcolor 
FROM (
    SELECT Beer.Beer, Beer.Brewery, Beer.Style, Beer.ABV, Beer.Hops, Beer.SRM, 
     Sum(BeerStock.Quantity) SummedQty 
    FROM Beer 
     INNER JOIN BeerStock 
      ON Beer.Beer = BeerStock.Beer 
    ) t 

小心使用BETWEEN - 它可能不會返回您想要的結果。我更喜歡使用大於和小於。我還將您的JOIN語法轉換爲INNER JOIN