PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come posso rendere compatibile questa query SQL di geo-distanza Postgres

La query utilizza la sintassi specifica per MySql. In Postgres (e in tutti gli altri RDBMS a me conosciuti) dovresti usare una tabella derivata:

select *
from (
    select *, 
        (3959 * acos( cos( radians(53.49) ) * cos( radians( places.lat ) ) 
        * cos( radians( places.lng ) - radians(-2.38) ) 
        + sin( radians(53.49) ) * sin( radians( places.lat ) ) ) ) AS distance 
    from (
        select *
        from places
        where places.lat between 53.475527714192 and 53.504472285808
        and places.lng between -2.4043246788967 and -2.3556753211033
        ) as places 
    where places.deleted_at is null 
    ) sub
where distance <= $1 
order by distance asc

Ho anche rimosso le virgolette dalle costanti numeriche.