相关疑难解决方法(0)

使用row_to_json进行Postgres递归查询

我在postgres 9.3.5中有一个表格,如下所示:

CREATE TABLE customer_area_node
(
  id bigserial NOT NULL,
  customer_id integer NOT NULL,
  parent_id bigint,
  name text,
  description text,

  CONSTRAINT customer_area_node_pkey PRIMARY KEY (id)
)
Run Code Online (Sandbox Code Playgroud)

我查询:

WITH RECURSIVE c AS (
       SELECT *, 0 as level, name as path FROM customer_area_node WHERE customer_id = 2 and parent_id is null
       UNION ALL
       SELECT customer_area_node.*, 
       c.level + 1 as level, 
       c.path || '/' || customer_area_node.name as path
  FROM customer_area_node 
  join c ON customer_area_node.parent_id = c.id
)
SELECT * FROM c ORDER …
Run Code Online (Sandbox Code Playgroud)

sql postgresql recursion json postgresql-json

6
推荐指数
2
解决办法
4230
查看次数

标签 统计

json ×1

postgresql ×1

postgresql-json ×1

recursion ×1

sql ×1