Oracle 使用正确的过滤器
Oracle using correct filter
我得到这个SQL-到目前为止的声明:
select *
from
table1,
table2
where table1.fk = table2.ID
AND table2.id = 4
这给了我以下输出:
[...] [...] Rights [...] [...]
... ... ( ID IN ( 10 , 700000 , 80 , 5 ) ) ... ...
... ... ( ID IN ( 500000 , 10 , 80 , 5 ) ) ... ...
... ... ( ID IN ( 10 , 5 , 80 , 900000 ) ) ... ...
我需要在我的 where
中写入,它只显示 Rights
中数字为 700000
或以下的行。
在这种情况下,只应显示 row
1
和 2
。
有什么解决办法吗?
这可能会帮助您确定需要做什么:
with t1 as (select 1 fk from dual union all
select 2 fk from dual union all
select 3 fk from dual),
t2 as (select 1 id, 'ID IN ( 10 , 700000 , 80 , 5 )' rights from dual union all
select 2 id, 'ID IN ( 500000 , 10 , 80 , 5 )' rights from dual union all
select 3 id, 'ID IN ( 10 , 5 , 80 , 900000 , 203 )' rights from dual),
pivoted_t2 as (select t2.id,
t2.rights,
rtrim(ltrim(UPPER(t2.rights), 'ID N('), ' )') rights_stripped,
to_number(trim(regexp_substr(rtrim(ltrim(UPPER(t2.rights), 'ID N('), ' )'), '[^,]+', 1, level))) rights_item,
max(level) over (partition by t2.id) num_items
from t2
connect by prior t2.id = t2.id
and prior dbms_random.value is not null
and trim(regexp_substr(rtrim(ltrim(UPPER(t2.rights), 'ID N('), ' )'), '[^,]+', 1, level)) is not null)
select t1.fk,
pt2.rights
from t1
inner join pivoted_t2 pt2 on (t1.fk = pt2.id)
where pt2.rights_item <= 700000
group by t1.fk,
pt2.rights,
pt2.num_items
having count(*) = pt2.num_items;
FK RIGHTS
---------- ------------------------------------
1 ID IN ( 10 , 700000 , 80 , 5 )
2 ID IN ( 500000 , 10 , 80 , 5 )
基本上,您需要将权限拆分为各个项目,计算出每组权限总共有多少个项目,然后计算出有多少低于您指定的值。如果返回的值与集合中的值数量相同,那么您就知道所有项目都符合您的过滤器。
ETA:我不能保证这会表现良好,尤其是对于大量数据,但如果您要坚持以这种方式存储数据,则必须接受这种性能很可能会受苦。
我得到这个SQL-到目前为止的声明:
select *
from
table1,
table2
where table1.fk = table2.ID
AND table2.id = 4
这给了我以下输出:
[...] [...] Rights [...] [...]
... ... ( ID IN ( 10 , 700000 , 80 , 5 ) ) ... ...
... ... ( ID IN ( 500000 , 10 , 80 , 5 ) ) ... ...
... ... ( ID IN ( 10 , 5 , 80 , 900000 ) ) ... ...
我需要在我的 where
中写入,它只显示 Rights
中数字为 700000
或以下的行。
在这种情况下,只应显示 row
1
和 2
。
有什么解决办法吗?
这可能会帮助您确定需要做什么:
with t1 as (select 1 fk from dual union all
select 2 fk from dual union all
select 3 fk from dual),
t2 as (select 1 id, 'ID IN ( 10 , 700000 , 80 , 5 )' rights from dual union all
select 2 id, 'ID IN ( 500000 , 10 , 80 , 5 )' rights from dual union all
select 3 id, 'ID IN ( 10 , 5 , 80 , 900000 , 203 )' rights from dual),
pivoted_t2 as (select t2.id,
t2.rights,
rtrim(ltrim(UPPER(t2.rights), 'ID N('), ' )') rights_stripped,
to_number(trim(regexp_substr(rtrim(ltrim(UPPER(t2.rights), 'ID N('), ' )'), '[^,]+', 1, level))) rights_item,
max(level) over (partition by t2.id) num_items
from t2
connect by prior t2.id = t2.id
and prior dbms_random.value is not null
and trim(regexp_substr(rtrim(ltrim(UPPER(t2.rights), 'ID N('), ' )'), '[^,]+', 1, level)) is not null)
select t1.fk,
pt2.rights
from t1
inner join pivoted_t2 pt2 on (t1.fk = pt2.id)
where pt2.rights_item <= 700000
group by t1.fk,
pt2.rights,
pt2.num_items
having count(*) = pt2.num_items;
FK RIGHTS
---------- ------------------------------------
1 ID IN ( 10 , 700000 , 80 , 5 )
2 ID IN ( 500000 , 10 , 80 , 5 )
基本上,您需要将权限拆分为各个项目,计算出每组权限总共有多少个项目,然后计算出有多少低于您指定的值。如果返回的值与集合中的值数量相同,那么您就知道所有项目都符合您的过滤器。
ETA:我不能保证这会表现良好,尤其是对于大量数据,但如果您要坚持以这种方式存储数据,则必须接受这种性能很可能会受苦。