簡體   English   中英

Oracle SQL 查詢與多列使用相同的子查詢與 OR

[英]Oracle SQL query with multiple columns using same subquery with OR

需要幫助簡化下面的 Oracle SQL 查詢與 OR 條件:是否可以使用單個 select 查詢而不是多個查詢。

對於這個子查詢“從 usr 中選擇 usr_key,其中 usr_login='abcd'”,我能夠將其變為單個而不是寫入 4 次,因為它只給出一個值。 但是對於第二個子查詢,它正在寫入多個值,因此我無法將它們組合起來。

select * from catalog  where 
(
(select usr_key from usr where usr_login='abcd') in (approver_user,CERTIFIER_USER,FULFILLMENT_USER) 
or ('abcd') in (EMERGENCYAPPROVERUSER,SPECIALAPPROVERUSER)
or approver_role IN 
(select ugp.ugp_key from ugp , usg,usr uuu where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd')
or CERTIFIER_ROLE IN 
(select ugp.ugp_key from ugp , usg,usr uuu where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd')
or FULFILLMENT_ROLE IN 
(select ugp.ugp_key from ugp , usg,usr uuu where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd')
)
and entity_type='Entitlement';


使用 WITH 子句

WITH some_query AS(
    select ugp.ugp_key from ugp , usg,usr uuu 
    where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd'
)
select * from catalog  
where 
(
    (select usr_key from usr where usr_login='abcd') in (approver_user,CERTIFIER_USER,FULFILLMENT_USER) 
    or 
    ('abcd') in (EMERGENCYAPPROVERUSER,SPECIALAPPROVERUSER)
    or 
    approver_role IN    (SELECT * FROM some_query)
    or 
    CERTIFIER_ROLE IN   (SELECT * FROM some_query)
    or 
    FULFILLMENT_ROLE IN (SELECT * FROM some_query)
)
and entity_type='Entitlement';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM