Sql 如何从多个起点递归获取记录?

Sql 如何从多个起点递归获取记录?,sql,postgresql,Sql,Postgresql,我的问题是跟进这一点: 此查询提供X的所有子记录: with recursive tree as ( select stocklocationid, parentid from tableA where stocklocationid = X union all select child.tableAid, child.parentid from tableA child join tree parent on parent.tableAid= c

我的问题是跟进这一点:

此查询提供X的所有子记录:

with recursive tree as (
   select  stocklocationid, parentid
   from tableA
   where stocklocationid = X
   union all
   select child.tableAid, child.parentid
   from tableA child
     join tree parent on parent.tableAid= child.parentid
)
select *
from tree;
这很有效。。。但是,它只能处理一个起点X

我想用muliple语句点运行它

我有一个列表:
{12,5,17,18}
,在我的查询中,我需要将每个数字替换为X。我如何使其自动执行并删除重复项(如果有)


我想要一个包含12,5,17,18和所有这些属性的列表……

其中stocklocationid在(12,5,17,18)中
其中stocklocationid在(12,5,17,18)中