如何在Where子句中实现多个条件?

时间:2015-08-05 18:23:35

标签: sql sql-server-2008 tsql sap

我希望符合3个条件的文件,但3个条件有多个条件。

这是我的代码:

SELECT
t0.DocNum

FROM 
dbo.OIPF t0 inner join IPF2 t1 ON t0.DocEntry = t1.DocEntry
inner join IPF1 t2 on t0.DocEntry = t2.DocEntry

WHERE 
(t1.OhType = 'W' and t1.CostSum > 0) and
(t1.OhType = 'F' and T1.CostSum > 0) and
(t1.OhType = 'Q' and T1.CostSum > 0)

这给了我的结果。但对我来说,这就是我如何确保所有这3种成本类型的总数大于 0。

有人可以帮我吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

您有3个不同的标准。请尝试or,但不要and

SELECT
t0.DocNum

FROM 
dbo.OIPF t0 inner join IPF2 t1 ON t0.DocEntry = t1.DocEntry
inner join IPF1 t2 on t0.DocEntry = t2.DocEntry

WHERE

(t1.OhType = 'W' and t1.CostSum > 0) or
(t1.OhType = 'F' and T1.CostSum > 0) or
(t1.OhType = 'Q' and T1.CostSum > 0);

答案 1 :(得分:0)

SELECT
t0.DocNum
FROM dbo.OIPF t0 
inner join IPF2 t1 ON t0.DocEntry = t1.DocEntry
--inner join IPF1 t2 ON t0.DocEntry = t2.DocEntry
WHERE t1.OhType in ('W' , 'F', 'Q') and t1.CostSum > 0

你要做的就是这个。此外,我不知道为什么表IPF1正在加入,如果它没有被使用。

答案 2 :(得分:0)

如果我理解你的问题,Alex_Bender的回答是正确的。

表达它的另一种更简单的方法可能更容易理解:

SELECT
t0.DocNum

FROM 
dbo.OIPF t0 inner join IPF2 t1 ON t0.DocEntry = t1.DocEntry
inner join IPF1 t2 on t0.DocEntry = t2.DocEntry

WHERE t1.OhType IN ('W', 'F', 'Q')
AND t1.CostSum > 0
相关问题