【问题标题】:How do I retrieve data from multiple related tables in Postgres?如何从 Postgres 中的多个相关表中检索数据?
【发布时间】:2015-12-23 15:49:25
【问题描述】:

Postgres 9.4
4个表之间有多对多的关系。我创建了额外的表来实现关系:

CREATE TABLE "public"."relation" (
  "id" uuid NOT NULL DEFAULT uuid_generate_v4(),
  "table1" uuid NOT NULL,
  "table2" uuid,
  "table3" uuid,
  "table4" uuid,
  "approved" bool DEFAULT true,
  CONSTRAINT "relation_pkey" PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE,
  CONSTRAINT "table1" FOREIGN KEY ("table1") REFERENCES "public"."table1" ("id") ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE,
  CONSTRAINT "table2" FOREIGN KEY ("table2") REFERENCES "public"."table2" ("id") ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE,
  CONSTRAINT "table3" FOREIGN KEY ("table3") REFERENCES "public"."table3" ("id") ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE,
  CONSTRAINT "table4" FOREIGN KEY ("table4") REFERENCES "public"."table4" ("id") ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
)
WITH (OIDS=FALSE);
ALTER TABLE "public"."relation" OWNER TO "postgres";

我需要从table1 中检索一行,包括来自其他表的所有相关行作为 JSON 对象。 在这里,我从relation 表中检索了行:

SELECT t.*
   FROM ( SELECT table1.id,
            (select row_to_json(relations.*) as array_to_json
              from(select * from relation where table1 = table1.id) relations
            ) as relations,

           from public.table1) t

但我不知道如何通过relation 表中的数据有效地从相关表中检索行。

可能这是有价值的信息:
relation 表中的每一行仅包含两个关系。例如,它可能包含与 table1 和 table2 的关系。其余列是空的(当然,id 除外)。 表 table1,2,3,4 中的每一行的关系少于 10 个。
我想检索这样的东西:

{
    id: table1.id,
    name: table1.name,
    related_items: [
        {id: table2.id, name: table2.name},
        {id: table4.id, name: table4.name},
        {id: table3.id, name: table3.name},
        {id: table2.id, name: table2.name},
        {id: table3.id, name: table3.name},
    ]
}

感谢您的宝贵时间!

【问题讨论】:

  • 你想1.使用关系表从所有4个相关表中提取数据,2.全部在一条记录上,然后3.使用row_to_json函数获取等效的JSON吗?跨度>
  • 我已经在描述中添加了答案
  • 试试这个帖子 - stackoverflow.com/questions/13227142/…。它包含很多关于你想要做什么的有用信息

标签: sql postgresql


【解决方案1】:
SELECT ROW_TO_JSON(T)
FROM    (
        SELECT t1.id,
               t1.name,
               array_to_json(array((
                 select case when t2.id is not null then row_to_json(t2) 
                             when t3.id is not null then row_to_json(t3) 
                             when t4.id is not null then row_to_json(t4) 
                        end
                 from   public.relation r 
                 LEFT JOIN public.table1 t2 on r.table2 = t2.id
                 LEFT JOIN public.table1 t3 on r.table3 = t3.id
                 LEFT JOIN public.table1 t4 on r.table4 = t4.id
                 where  r.table1 = t1.id
               ))) related_items
        FROM   public.table1 t1
       ) T

【讨论】:

    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多