Postgresql return 变量类型存储在 table

Postgresql return variable in type stored in a table

我目前正在开发一个应用程序,在尝试对使用自定义类型的 table 创建查询时遇到了问题。

这是一个示例table并输入,

CREATE TYPE address AS (
  street varchar(40),
  city   varchar(25),
  zip    varchar(5)
);
CREATE TABLE houses (
  id SERIAL PRIMARY KEY,
  address address
);

我需要检索一个列表,其中包含存储在 table 房屋中的所有城市。 这是我尝试做的事情:

SELECT address.city
FROM houses
GROUP BY address.city

它将 address.city 检测为 table。有什么方法可以在 PostgreSQL 中做我想做的事吗?

我不会在 table 中使用复合类型。但是你仍然可以这样做,使用括号。

SELECT (address).city
FROM houses
GROUP BY (address).city

来自the docs

To access a field of a composite column, one writes a dot and the field name, much like selecting a field from a table name. In fact, it's so much like selecting from a table name that you often have to use parentheses to keep from confusing the parser.

相反,我会使用 jsonb。或者,如果您要使用复合类型,则应使用 PostGIS 中的 stdaddr