如何在层次结构 table 中查找任何内容?
How to find anything in a level-structured table?
我无法查询层次结构 table:
select *
from user_info
where user = 1;
user level value
1 1 Jackson
1 2 Pollock
1 3 1989-02-03
1 4 male
1 5 single
如果我想获得与用户 1 在级别 1、2、3 和 4 中具有完全相同值的所有用户怎么办?
可能最简单的方法是listagg()
:
with u as (
select u.user, listagg(value, '|') within group (order by level) as vals
from user_info u
where level between 1 and 4
)
select
from u join
u1
on u.vals = u1.vals and u1.user = 1;
更传统的方法是自连接:
select u.user
from user_info u join
user_info u1
on u1.level = u.level and
u1.value = u.value and
u1.user = 1
where u1.level between 1 and 4
group by u.user
having count(*) = 4; -- four levels match
我无法查询层次结构 table:
select *
from user_info
where user = 1;
user level value
1 1 Jackson
1 2 Pollock
1 3 1989-02-03
1 4 male
1 5 single
如果我想获得与用户 1 在级别 1、2、3 和 4 中具有完全相同值的所有用户怎么办?
可能最简单的方法是listagg()
:
with u as (
select u.user, listagg(value, '|') within group (order by level) as vals
from user_info u
where level between 1 and 4
)
select
from u join
u1
on u.vals = u1.vals and u1.user = 1;
更传统的方法是自连接:
select u.user
from user_info u join
user_info u1
on u1.level = u.level and
u1.value = u.value and
u1.user = 1
where u1.level between 1 and 4
group by u.user
having count(*) = 4; -- four levels match