1

I'm trying to compare two values against the values in my table to find the closest one, but each time I try the code (see below) I just get the first row returned

    SELECT latitude, longitude, ABS(latitude - 53.316494), ABS(longitude - -6.253541) AS distance
    FROM station
    ORDER BY distance
    LIMIT 1;

Thanks for your help.

d199224
  • 531
  • 2
  • 8
  • 23

1 Answers1

0

Your query:

SELECT latitude, longitude, ABS(latitude - 53.316494), ABS(longitude - -6.253541)
FROM station
LIMIT 1;

But "Compare closest value in MySQL" in latitude and longitude means to find closest way Should looks like:

SELECT latitude, longitude, ABS(latitude - 53.316494), ABS(longitude - -6.253541)
FROM station
ORDER BY (POW((latitude - 53.316494,2) + POW((longitude - -6.253541),2)) ASC
LIMIT 1;

enter image description here

Andrzej Reduta
  • 767
  • 1
  • 7
  • 15