一种方法是使用连接和子查询:
select c.*
from coords c join
(select min(x) as xmin, max(x) as xmax, min(y) as ymin, max(y) as ymax
from coords
) clim
on c.x in (clim.xmin, clim.xmax) or c.y in (clim.ymin, clim.ymax)
另一种方法是使用窗口函数:
select c.*
from (select c.*,
least(row_number() over (order by x),
row_number() over (order by x desc),
row_number() over (order by y),
row_number() over (order by y desc)
) as seqnum
from coords c
) c
where seqnum = 1
另一种方式,如果你真的只想要 4 分是:
select * from coords order by x limit 1 union all
select * from coords order by x desc limit 1 union all
select * from coords order by y limit 1 union all
select * from coords order by y desc limit 1
对于如此多的行,如果您在 x 和 y 上有索引,这些都会运行得更快。在这种情况下,最后一个可能是最快的。