答案 0 :(得分:1)
使用ORDER BY
添加coalesce()
:
WITH RECURSIVE tag_tree AS (
(
SELECT * FROM "Tag" WHERE "TagId" = 1
)
UNION ALL
SELECT child.* FROM "Tag" child
JOIN tag_tree parent on parent."TagId" = child."ParentTagId"
)
SELECT * FROM tag_tree
ORDER BY coalesce("ParentTagId", 0), "Name";
TagId | ParentTagId | Name
-------+-------------+------
1 | | a
4 | 1 | a
3 | 1 | b
5 | 3 | a
(4 rows)
COALESCE函数返回其第一个参数 空值。仅当所有参数都为null时才返回Null。经常这样 用于在数据时用空值替换默认值 检索显示。
在这种情况下,该功能会将null
更改为0
。