我在 Postgres 中有一个自定义类型:

CREATE TYPE child AS
   (id integer,
    name text,
    surname text,
    age integer);

和 table
CREATE TABLE parent
(
  id integer NOT NULL,
  name text NOT NULL,
  surname text NOT NULL,
  age integer NOT NULL,
  childs child[],
  CONSTRAINT parent_pkey PRIMARY KEY (id)
)

我想让 parent 有一个名字=“约翰”的 child

我试过这样的事情:
select id, name
  from parent
where 'John' = any  (select (unnest(childs)).name from parent)

但我收到了所有的 parent 。
当有人解决我的问题时,我会非常感激。

最佳答案

您必须以某种方式关联第一个父对象和第二个父对象。您可以使用 Vao Tsun 的查询或尝试使用相关查询:

select id, name
  from parent as p1
where 'John' = any  (select (unnest(childs)).name from parent as p2 where p1.id = p2.id)

关于PostgreSQL 中自定义类型的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40873422/

10-10 09:23
查看更多