出于某种原因,我无法将结果设置为限制只有价格状态为“正常”的产品。当我使用像where pricestatus like 'normal'
这样的where子句时,结果集也会过滤其他记录。这里有什么想法吗?
SELECT
od.order_id,
ISNULL(p.pricestatus,'normal') as pricestatus,
ISNULL(od.partnumber,'unknown') as partnumber,
od.product_id,
od.producttitle,
qty,
od.price,
extprice,
customfield1 AS prodstatus,
ISNULL(categorytitle,'-') AS categorytitle,
needbydate,
customfield2 AS vendor,
(SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 1) AS color,
(SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 2) AS size,
od.producttitle + ISNULL((SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 1),'') + ISNULL((SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 2),'') + Cast(od.price AS VARCHAR(15)) AS pkey
FROM
orderdetails od
INNER JOIN orders ord ON ord.order_id = od.order_id
LEFT OUTER JOIN products p ON p.product_id = od.product_id
LEFT OUTER JOIN objectgroups ob ON p.objectgroup_id = ob.objectgroup_id
LEFT OUTER JOIN categories c ON ob.category_id = c.category_id
WHERE
1=1
AND ord.shippingdate BETWEEN {ts '2009-10-05 00:01:00'} AND {ts '2009-10-21 23:59:59'}
AND ISNULL(ord.status,0) IN (5,12)
AND ISNULL(ord.status,0) <> 6
AND ISNULL(ord.status,0) <> 10
AND ISNULL(ord.status,0) <> 7
AND ISNULL(ord.status,0) <> 8
ORDER BY
pkey
答案 0 :(得分:3)
您正在对产品进行LEFT OUTER JOIN。请记住,如果在WHERE子句中放置一个条件,它将把该条件应用于WHOLE集,因此任何不带回产品的行都将自动被过滤掉,因为您的条件不能成立。
您需要将条件添加到您的加入中:
LEFT OUTER JOIN products p
ON p.product_id = od.product_id AND p.priceStatus LIKE "%normal%'
答案 1 :(得分:1)
数据是什么样的?除非数据完全是文本正常,否则您将不会选择正常的变体。
请尝试以下
WHERE priceStatus LIKE "%normal%'
如果您还想要NULL记录,请使用
WHERE PriceStatus LIKE '%normal%' or PriceStatus is NULL
答案 2 :(得分:0)
我认为您需要将其添加到where子句的末尾
and isnull(p.pricestatus,'normal') = 'normal'
像这样
SELECT od.order_id,
Isnull(p.pricestatus,'normal') as pricestatus,
Isnull(od.partnumber,'unknown') as partnumber,
od.product_id,
od.producttitle,
qty,
od.price,
extprice,
customfield1 AS prodstatus,
Isnull(categorytitle,'-') AS categorytitle,
needbydate,
customfield2 AS vendor,
(SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 1) AS color,
(SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 2) AS size,
od.producttitle + Isnull((SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 1),'') + Isnull((SELECT opttitle
FROM options o
WHERE Charindex(',' + Cast(o.opt_id AS VARCHAR) + ',',',' + opt_ids + ',') > 0
AND optlevel = 2),'') + Cast(od.price AS VARCHAR(15)) AS pkey
FROM orderdetails od
INNER JOIN orders ord
ON ord.order_id = od.order_id
LEFT OUTER JOIN products p
ON p.product_id = od.product_id
LEFT OUTER JOIN objectgroups ob
ON p.objectgroup_id = ob.objectgroup_id
LEFT OUTER JOIN categories c
ON ob.category_id = c.category_id
WHERE 1=1
and isnull(p.pricestatus,'normal') = 'normal'
AND ord.shippingdate BETWEEN {ts '2009-10-05 00:01:00'} AND {ts '2009-10-21 23:59:59'}
AND Isnull(ord.status,0) IN (5,12)
AND Isnull(ord.status,0) <> 6
AND Isnull(ord.status,0) <> 10
AND Isnull(ord.status,0) <> 7
AND Isnull(ord.status,0) <> 8
ORDER BY pkey