我如何结合OR&在SQL中?在下面的查询中,我需要条件语句daction为'Reject',所有值OR daction为'Approve',其'APPROVALTYPE'为NOT NULL值
以下是我的询问:
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);
请帮帮我。
答案 0 :(得分: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 :(得分:1)
使用括号:
AND
(
daction = 'Reject'
OR
(daction ='Approve' AND ApprovalType.ApprovalType IS NOT NULL)
)
这将为您提供具有批准类型和所有拒绝的所有批准。
但是,请检查您的联接。有三个表,但我没有看到所有这些表的连接。这可能是故意的(期望的交叉连接),但最好检查一下。 (无论如何使用ANSI连接语法以避免意外的交叉连接并提高可读性会更好。)我认为daction和dDocName都不在表ApprovalType中?否则你会破坏你的外连接。使用多个表时,使用限定符来显示字段的来源 - 再次避免错误并提高可读性。只是我的5c: - )