Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Postgres的顺序是两次描述,然后是ASC_Sql_Postgresql_Sql Order By - Fatal编程技术网

Sql Postgres的顺序是两次描述,然后是ASC

Sql Postgres的顺序是两次描述,然后是ASC,sql,postgresql,sql-order-by,Sql,Postgresql,Sql Order By,我有一个递归查询 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:尝试了此解决方案: 给我一个错误: ERROR: su

我有一个递归查询

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:尝试了此解决方案:

给我一个错误:

ERROR:  subquery in FROM must have an alias 

在PostgreSQL中,所有表表达式(在中的
之后选择)都需要命名。也就是说,他们需要有一个别名。只需添加任何别名。例如,我添加了
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 

你发现了吗?

你能解释一下所需的顺序吗?我尝试了那个解决方案,但它不起作用,这就是我为什么发帖的原因。错误:FROM中的子查询必须有别名谢谢你指出了这一点
select * from (SELECT * from tree ORDER BY comment_id DESC LIMIT 5) t order by comment_id    
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