繁体   English   中英

如何为下面的SQL查询传递单个参数

[英]How to Pass Single parameter for below SQL query

我已经两次传递参数(:PRODUCT_ID)。 我如何通过:PRODUCT_id只有一次以下查询

select count(1)
from (
    select count(1) album_fa_counter
    from actual_configs ac
    where ac.config_id = :PRODUCT_ID
        and exists (
            select 1
            from config_participants cp
            where CONTRIBUTOR_CATEGORY = 'Featured Artist'
                and cp.gpid = ac.gpid
            )
    ) a,
    (
        select count(1) matching_track_fa_counter
        from actual_tracks at1,
            actual_configs ac1
        where at1.gpid = ac1.gpid
            and ac1.config_id = :PRODUCT_ID
            and exists (
                select 1
                from recording_participants rp,
                    config_participants cp
                where cp.CONTRIBUTOR_CATEGORY = 'Featured Artist'
                    and cp.gpid = at1.gpid
                    and cp.participant_name = rp.participant_name
                    and rp.CONTRIBUTOR_CATEGORY = 'Featured Artist'
                    and rp.isrc = at1.isrc
                )
        ) b
where a.album_fa_counter = 0
    or b.matching_track_fa_counter > 0;

我会尝试解决这个问题:

With
a as
 (select count(1) album_fa_counter
from actual_configs ac
where ac.config_id = :PRODUCT_ID
    and exists (
        select 1
        from config_participants cp
        where CONTRIBUTOR_CATEGORY = 'Featured Artist'
            and cp.gpid = ac.gpid),

b as
 (
    select count(1) matching_track_fa_counter
    from actual_tracks at1,
        actual_configs ac1,
        a
    where at1.gpid = ac1.gpid
        and ac1.config_id = ac.config_id
        and exists (
            select 1
            from recording_participants rp,
                config_participants cp
            where cp.CONTRIBUTOR_CATEGORY = 'Featured Artist'
                and cp.gpid = at1.gpid
                and cp.participant_name = rp.participant_name
                and rp.CONTRIBUTOR_CATEGORY = 'Featured Artist'
                and rp.isrc = at1.isrc
            )
Select count (1)
from a,b
Where 
a.album_fa_counter = 0
or b.matching_track_fa_counter > 0;

现在数据库将a和b视为真实表,因此在a中使用的参数应该被视为表中的正常可用值。 希望这对你有用。

如果不对代码进行太多修改,可以通过使用CTE来包含变量,然后在查询中使用它来加入变量:

with yourVariable(val) as ( select :PRODUCT_ID from dual)
select count(1)
from (
    select count(1) album_fa_counter
    from actual_configs ac,
         yourVariable
    where ac.config_id = yourVariable.val
        and exists (
            select 1
            from config_participants cp
            where CONTRIBUTOR_CATEGORY = 'Featured Artist'
                and cp.gpid = ac.gpid
            )
    ) a,
    (
        select count(1) matching_track_fa_counter
        from actual_tracks at1,
            actual_configs ac1,
            yourVariable
        where at1.gpid = ac1.gpid
            and ac1.config_id = yourVariable.val
            and exists (
                select 1
                from recording_participants rp,
                    config_participants cp
                where cp.CONTRIBUTOR_CATEGORY = 'Featured Artist'
                    and cp.gpid = at1.gpid
                    and cp.participant_name = rp.participant_name
                    and rp.CONTRIBUTOR_CATEGORY = 'Featured Artist'
                    and rp.isrc = at1.isrc
                )
        ) b
where a.album_fa_counter = 0
    or b.matching_track_fa_counter > 0;

顺便说一句,我编辑了你的代码而没有重写它,但是做一点努力并用ANSI SQL重写它会好得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM