如何在WHERE子句

时间:2016-01-19 04:06:24

标签: mysql sql

如何在WHERE中使用IF语句?下面的代码不起作用

SELECT item.item_id, 
       item.item_name, 
       item.item_qty 
IF(sell.product_qty IS NULL, item.item_qty, item.item_qty - SUM(sell.product_qty)),
       sell.date_order, 
       sell.date_shipping
FROM item
     OUTER JOIN sell
                ON item.item_id = sell.product_item_id
WHERE NOW() BETWEEN sell.date_order AND sell.date_shipping

1 个答案:

答案 0 :(得分:0)

您应该使用CASE语句。

CASE WHEN sell.product_qty IS NULL THEN item.item_qty 
ELSE item.item_qty - SUM(sell.product_qty) END