MySQL Case / If / Then

时间:2013-04-01 14:16:48

标签: mysql sql case

我正在尝试在MySQL中构建一个查询,我有一个数字库存水平的东西,并且根据库存水平是什么,我希望它返回另一个颜色代码值。

E.g。如果库存水平<0,那么EMPTY如果库存水平介于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的基础知识,但不是这种东西!

任何建议/指示赞赏......

由于

安迪

2 个答案:

答案 0 :(得分:9)

我很瘦你的意思是,如果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 :(得分:0)

这样的事情应该使用CASE声明:

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