给出以下Select Statement
String cmpquery = "SELECT cwfour.ord_number, cwfour.ord_status,
orderheader.ord_status
FROM live.dbo.cwfour cwfour, live.dbo.orderheader orderheader
WHERE ((cwfour.ord_number = orderheader.ord_hdrnumber AND NOT
cwfour.ord_status='CAN')) or ((cwfour.ord_number = orderheader.ord_hdrnumber
AND NOT cwfour.ord_status='CMP'))"
结果集显示文件中的所有订单,但我只要求那些不是CAN或CMP,取消或完成的订单。我的where子句中缺少什么?
答案 0 :(得分:1)
您正在查询状态为“无法”的行。或者不是CMP'它将返回所有行,因为' CAN'问题' CMP'反之亦然。相反,您应该查询行也不是。 E.g:
SELECT cwfour.ord_number, cwfour.ord_status, orderheader.ord_status
FROM live.dbo.cwfour cwfour, live.dbo.orderheader orderheader
WHERE cwfour.ord_number = orderheader.ord_hdrnumber AND
cwfour.ord_status NOT IN ('CAN', 'CMP')
值得注意的是,隐式连接(在from
子句中列出多个表)已经被认为已经过时很长时间了。相反,建议使用显式连接:
SELECT cwfour.ord_number, cwfour.ord_status, orderheader.ord_status
FROM live.dbo.cwfour cwfour
JOIN live.dbo.orderheader orderheader ON
cwfour.ord_number = orderheader.ord_hdrnumber
WHERE cwfour.ord_status NOT IN ('CAN', 'CMP')
答案 1 :(得分:0)
逻辑!
你的条件是:
(cwfour.ord_number = orderheader.ord_hdrnumber AND NOT cwfour.ord_status='CAN')
OR
(cwfour.ord_number = orderheader.ord_hdrnumber AND NOT cwfour.ord_status='CMP')
A AND !B
OR
A AND !C
哪个是
A & (!B | !C)
简单地说:A & (almost anything)
;)