Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive query in PostgreSQL. SELECT *

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;

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?

like image 375
WelcomeTo Avatar asked Apr 29 '15 12:04

WelcomeTo


1 Answers

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;
like image 168
a_horse_with_no_name Avatar answered Oct 27 '22 06:10

a_horse_with_no_name