Postgres如何在查询中使用点类型

Postgres how to use point type in query

你好,我有一个 Postgres SQL table,它的一列是类型 point

CREATE TABLE public.geometry_cylinder
(
    id          serial not null primary key,
    distance    float not null,
    height      float not null,
    coordinates point not null

);

如何在 SQL 查询中使用 x 或 y 坐标之一?

select * from public.geometry_cylinder where coordinates.x > 14.24

point 不是记录类型,因此您无法使用点符号访问 xy(尽管我承认,这是合乎逻辑的)。关于如何操作的说明有点隐藏 in the manual:

It is possible to access the two component numbers of a point as though the point were an array with indexes 0 and 1. For example, if t.p is a point column then SELECT p[0] FROM t retrieves the X coordinate

所以你可以使用:

select * 
from public.geometry_cylinder 
where coordinates[0] > 14.24;