MySQL Case/If/Then

前端 未结 2 2040
温柔的废话
温柔的废话 2021-01-12 17:38

I am trying to build a query in MySQL, where I have a numeric stock level of something and depending on what that stock level is, I want it to return another value that woul

相关标签:
2条回答
  • 2021-01-12 18:06

    I thin you mean that if the stock > 1000 then another color 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..
    

    but if you really mean that,

    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..
    

    One more thing, You also need to use GROUP BY clause or else you will be returning only one record even if you have different records,

    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
    
    0 讨论(0)
  • 2021-01-12 18:07

    Something like this should work using the CASE statement:

    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
    

    Be careful using BETWEEN -- it may not return your desired results. I prefer using greater than and less than. I also converted your JOIN syntax to an INNER JOIN.

    0 讨论(0)
提交回复
热议问题