2014-01-17 52 views
0

如何在SQL中組合OR &?在下面的查詢,我所需要的條件語句daction是「拒絕」的所有值或daction是「批准」有「APPROVALTYPE」爲NOT NULL值在Oracle中組合或者&SQL

下面是我的查詢:

SELECT WorkflowHistory.*, 
     Reason.Reason, 
     ApprovalType.ApprovalType 
FROM WorkflowHistory WorkflowHistory, 
     Reason Reason, 
     ApprovalType ApprovalType 
WHERE UPPER(dDocName) = UPPER('D_1188259') 
AND xDocApproval = ApprovalType.ApprovalTypeID(+) 
AND daction like 'Reject' 
OR (daction ='Approve' 
AND APPROVALTYPE IS NOT NULL); 

請幫幫我。

回答

2

這樣

SELECT WorkflowHistory.*, 
Reason.Reason, 
ApprovalType.ApprovalType 
FROM WorkflowHistory WorkflowHistory, 
Reason Reason, 
ApprovalType ApprovalType 
WHERE UPPER(dDocName) = UPPER('D_1188259') 
AND xDocApproval  = ApprovalType.ApprovalTypeID(+) 
AND (daction   like 'Reject' 
      OR (daction ='Approve' AND APPROVALTYPE IS NOT NULL)); 
1

使用括號:

AND 
(
    daction = 'Reject' 
OR 
    (daction ='Approve' AND ApprovalType.ApprovalType IS NOT NULL) 
) 

這使您有一個批准類型以及所有拒絕所有批准。

但是,請檢查您的連接。有三張表,但我沒有看到所有這些表的連接。這可能是故意的(所需的交叉連接),但最好檢查一下。 (最好使用ANSI連接語法來避免意外的交叉連接並增強可讀性。)我認爲表格ApprovalType中既沒有daction也沒有dDocName?否則,你會破壞你的外連接。處理多個表格時,使用限定符來顯示字段的來源 - 再次避免錯誤並增強可讀性。只是我的5c :-)