我对创建一个从两个不同的列中提取值的case语句感兴趣,其中一个涉及计算。.我不确定如何进行此工作,但我将向您展示到目前为止的内容。 (我有一栏显示的是商品类型,例如特殊订单商品,停产的商品和通常库存的商品。第二栏显示的是手头数量-销售订单上的数量,以确定该商品是否有库存。)我的查询

SELECT ItemID, ItemType, (QuantityOnHand - QuantityonSalesOrders) AS Instock
CASE
WHEN ItemType = 'SP / OR' THEN 'Special Order'
WHEN Instock < '1' THEN 'Out of Stock'
WHEN Instock > '0' THEN 'In Stock'
AS "Stock" FROM peachtree;

最佳答案

对于这种类型的请求,您将不得不重用计算或使用子查询。这是因为您要提供的Instock值的别名在选择列表中不可用:

SELECT ItemID,
    ItemType,
    Instock,
    CASE
        WHEN ItemType = 'SP / OR' THEN 'Special Order'
        WHEN Instock < '1' THEN 'Out of Stock'
        WHEN Instock > '0' THEN 'In Stock'
    END AS "Stock"
FROM
(
    select ItemID, ItemType, (QuantityOnHand - QuantityonSalesOrders) AS Instock
    from peachtree
) p;

或者:
SELECT ItemID,
    ItemType,
    (QuantityOnHand - QuantityonSalesOrders) as Instock,
    CASE
        WHEN ItemType = 'SP / OR' THEN 'Special Order'
        WHEN (QuantityOnHand - QuantityonSalesOrders) < '1' THEN 'Out of Stock'
        WHEN (QuantityOnHand - QuantityonSalesOrders) > '0' THEN 'In Stock'
    END AS "Stock"
FROM peachtree

关于mysql - Mysql CASE语句多列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14242097/

10-12 21:17