PostgreSQL中的递归查询。选择 * [英] Recursive query in PostgreSQL. SELECT *

查看:569
本文介绍了PostgreSQL中的递归查询。选择 *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个递归查询来检索给定人的所有孩子

I have recursive query to retrieve all children of a given person

WITH RECURSIVE recursetree(id, parent_id) AS (
    SELECT id, parent_id FROM tree WHERE parent_id = 0
  UNION
    SELECT t.id, t.parent_id
    FROM tree t
    JOIN recursetree rt ON rt.id = t.parent_id
  )
SELECT * FROM recursetree;

如您所见,我正在指定要检索的列的列表。但是我想使用 SELECT * (我在真实表中确实有很多列,以后可以更改它们)。是否有某种方法可以获取所有列而无需单独定义每个列?

As you can see, I'm specifying list of columns to be retrieved. But I want to use SELECT * (I have really many columns in real table, and they can be changed in future). Is there some way to get ALL columns without defining each column individually?

推荐答案

您无需在部分。如果不进行说明,则列名将由 UNION 中的第一个查询确定:

You don't need to specify the columns in the WITH part. If you leave that out, the column names will be determined by the first query in the UNION:

WITH RECURSIVE recursetree AS (
    SELECT * FROM tree WHERE parent_id = 0
  UNION
    SELECT t.*
    FROM tree t
    JOIN recursetree rt ON rt.id = t.parent_id
)
SELECT * 
FROM recursetree;

这篇关于PostgreSQL中的递归查询。选择 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆