Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres how to use point type in query

Hello I have an Postgres SQL table, one column of its is type point

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

);

how can I use one of x or y coordinates in an SQL query?

select * from public.geometry_cylinder where coordinates.x > 14.24
like image 671
chatzich Avatar asked Mar 14 '26 02:03

chatzich


1 Answers

point isn't a record type so you can't access x and y using the dot notation (though I admit, this would be logical). The explanation how to do it, is a bit hidden 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

So you can use:

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!