2010-05-11 223 views
1

如何过滤此查询以显示“停用= 1”和“数量= 0”的任何项目?我不想排除停用值为1的所有项目。如果该项目有停产= 1的值和= 0。sql server中的条件语句2k5

select sku,producttitle,location,pt_type as type, vendor_name, 
    active,pricestatus, disc as discontinued,season,yourprice as 
    customerprice,costprice,quantity,stockvalue 

    from getskus 

回答

2
WHERE NOT (discontinued = 1 AND quantity = 0) 

这代表正是你想要表达什么量才适用。您排除(关键字NOT)行满足(关键词AND)您所需的条件。

0
SELECT sku, 
     producttitle, 
     location, 
     pt_type AS TYPE, 
     vendor_name, 
     ACTIVE, 
     pricestatus, 
     disc AS discontinued, 
     season, 
     yourprice AS customerprice, 
     costprice, 
     quantity, 
     stockvalue 
FROM getskus 
WHERE NOT (disc = 1 AND quantity = 0) 

你也可以改变方向这样

SELECT sku, 
     producttitle, 
     location, 
     pt_type AS TYPE, 
     vendor_name, 
     ACTIVE, 
     pricestatus, 
     disc AS discontinued, 
     season, 
     yourprice AS customerprice, 
     costprice, 
     quantity, 
     stockvalue 
FROM getskus 
WHERE disc <> 1 
     OR quantity <> 0 

但我宁愿第一个

+0

你肯定第一个是更快?如果可以通过简单的布尔转换来优化查询计划,我会感到惊讶...... – Heinzi 2010-05-11 17:01:28

+0

对于说 – 2010-05-11 17:05:21

0
select sku 
     ,producttitle 
     ,location 
     ,pt_type as type 
     ,vendor_name 
     ,active 
     ,pricestatus 
     ,disc as discontinued 
     ,season 
     ,yourprice as customerprice 
     ,costprice 
     ,quantity 
     ,stockvalue 
from getskus 
WHERE NOT (disc = 1 and quantity= 0)