我有一个递归查询
With RECURSIVE tree AS
(
SELECT * FROM comments WHERE id = 83
UNION
SELECT t.*
From comments t
JOIN tree rt ON rt.parent_id = t.id
)
SELECT * from tree ORDER BY id DESC LIMIT 5
我将如何通过ASC再订购一次?
Edit1 :尝试了以下解决方案: Get the last N rows in the database in order?
With RECURSIVE tree AS
(
SELECT * FROM comments WHERE id = 83
UNION
SELECT t.*
From comments t
JOIN tree rt ON rt.parent_id = t.id
)
select * from (SELECT * from tree ORDER BY id DESC LIMIT 5) order by id
给我一个错误:
ERROR: subquery in FROM must have an alias
答案 0 :(得分:0)
select * from (SELECT * from tree ORDER BY comment_id DESC LIMIT 5) t order by comment_id
答案 1 :(得分:0)
在PostgreSQL中,所有表表达式(FROM
之后的选择)都需要命名。也就是说,他们需要有一个别名。只需添加任何别名即可。例如,我添加了x
,如下所示:
With RECURSIVE tree AS
(
SELECT * FROM comments WHERE id = 83
UNION
SELECT t.*
From comments t
JOIN tree rt ON rt.parent_id = t.id
)
select * from (SELECT * from tree ORDER BY id DESC LIMIT 5) x order by id
您发现了吗?