Where子句中的多个If语句

时间:2013-11-15 07:14:59

标签: sql oracle if-statement

我想合并我的2 If语句的条件结果。首先,如果语句结果只显示0结果,则在第二个if语句结果中,其中一个必须大于0 ...我想要做的是保留第二个条件,如果像我的代码那样的statament则先修改...

我的代码

If (Inventory) <> 0 Then 
    If Apple = "" And Banana = "" Then
        strSQL = strSQL & " AND (myApple = 0 AND myBanana = 0) + (myApple <> 0 OR myBanana <> 0)"
    End If
End If

//首先

If (Inventory) <> 0 Then 
    If Apple = "" And Banana = "" Then
        strSQL = strSQL & " AND (myApple = 0 AND myBanana = 0)"
    End If
End If

第一个结果:

myApple myBanana
 0       0
 0       0
 continue...

//二

If int(Inventory) <> -1 Then    
    If Apple = "" And Banana = "" Then
        strSQL = strSQL & " AND (myApple <> 0 OR myBanana <> 0)"
    End If
End If

第二个结果:

 myApple myBanana
     0       5
     1       0
     continue...

我想看到的结果:

myApple myBanana
     0       0
     0       0
     0       5
     1       0
     6       0
     0       0
  continue.....

1 个答案:

答案 0 :(得分:2)

尝试使用此技巧

If Apple = "" And Banana = "" Then
    strSQL = strSQL & " AND (1 = " + If (Inventory) <> 0 Then "1" else "0" + " AND (myApple = 0 AND myBanana = 0))"       
    strSQL = strSQL & " AND (1 = " + If (Inventory) <> -1 Then "1" else "0" + " AND(myApple <> 0 OR myBanana <> 0))"
End If

所以得到sqlwhere for Inventory = -1

AND (1 = 1 AND (myApple = 0 AND myBanana = 0)) AND (1 = 0 AND (myApple <> 0 OR myBanana <> 0))