如何从数据库中获取人的全名

时间:2018-08-31 15:10:35

标签: sql oracle11g hierarchical-data recursive-query

我有这些表

表,其中包含以下数据

person_id    description

 1          first in the family
 2          second in the family
 3          third in the family
 4          fourth int the family
 5          fifth in the family

人名表以及以下数据

person_id  first_name

  1        Santiago
  2         Lautaro
  3          Lucas
  4          Franco
  5          Agustín

父亲表,其中包含以下数据

person_father_id  description
    1              father of Lautaro
    2              father of Lucas
    3              father of Franco
    4              father of Agustín

儿童表,其中包含以下数据

 person_child_id   person_father_id
      2                 1
      3                 2
      4                 3
      5                 4

如何获取选择pl / sql查询中person_id 4的人的全名(AgustínFranco Lucas Lucas Lautaro Santiago)。核心表是人

1 个答案:

答案 0 :(得分:0)

您可以将层次结构查询与内联视图一起使用,该内联视图首先将相关表连接在一起。内联视图的查询可能是:

select p.person_id, pn.first_name, c.person_father_id
from person p
join person_name pn on pn.person_id = p.person_id
left join children c on c.person_child_id = p.person_id;

 PERSON_ID FIRST_NAME PERSON_FATHER_ID
---------- ---------- ----------------
         2 Lautaro                   1
         3 Lucas                     2
         4 Franco                    3
         5 Agustín                   4
         1 Santiago                   

并以此为基础进行层次查询:

select trim(sys_connect_by_path(first_name, ' ')) as whole_name
from (
  select p.person_id, pn.first_name, c.person_father_id
  from person p
  join person_name pn on pn.person_id = p.person_id
  left join children c on c.person_child_id = p.person_id
)
where connect_by_isleaf = 1
start with person_id = 4
connect by person_id = prior person_father_id;

WHOLE_NAME                                        
--------------------------------------------------
Franco Lucas Lautaro Santiago

或者您可以将层次查询本身作为进一步的子查询,然后再加入名称并进行汇总:

select listagg(pn.first_name, ' ') within group (order by lvl) as whole_name
from (
  select person_id, level as lvl
  from (
    select p.person_id, c.person_father_id
    from person p
    left join children c on c.person_child_id = p.person_id
  )
  start with person_id = 4
  connect by person_id = prior person_father_id
) t
join person_name pn on pn.person_id = t.person_id;

WHOLE_NAME                                        
--------------------------------------------------
Franco Lucas Lautaro Santiago

请注意,对于这两个表,您都必须先联接表,然后才能根据起始ID(start with而不是where进行过滤,因为这是分层的)。这意味着,使用更大的表可能会完成比您真正需要或期望更多的工作。

或者,如果愿意,也可以使用递归子查询分解(递归CTE)执行相同的操作,并且您使用的是Oracle 11gR2或更高版本:

with r (person_id, person_father_id, lvl) as (
  select p.person_id, c.person_father_id, 1
  from person p
  left join children c on c.person_child_id = p.person_id
  where p.person_id = 4
  union all
  select p.person_id, c.person_father_id, r.lvl + 1
  from r
  join person p on p.person_id = r.person_father_id
  left join children c on c.person_child_id = p.person_id
)
select listagg(pn.first_name, ' ') within group (order by lvl) as whole_name
from r
join person_name pn on pn.person_id = r.person_id;

WHOLE_NAME                                        
--------------------------------------------------
Franco Lucas Lautaro Santiago

看起来更复杂,但至少可以将过滤器放在递归CTE的锚点中。

详细了解hierarchical queriesrecursive subquery factoring