how to do a mysql query that delivers two columns and that one has the value of the next row

0

I need someone to help me in how to do a mysql query that delivers two columns and that one has the value of the next row.

I have the following records in this table:

and I need to do a SELECT which gives me in a column the values of lat starting from id 403 down, and in another column the values of lon but starting from id 401 downwards. is it understood?

Greetings, Matias

    
asked by Matias Navarrete 05.11.2017 в 23:04
source

1 answer

0

You could do a sub-query where:

  • Look for those records where the id is less than the record in question
  • Sorting descending by id
  • And limit the result to 1
  • So for example:

    SELECT A.lat, 
      (
         SELECT B.lon
         FROM 'myTable' AS B
         WHERE B.id < A.id
         ORDER BY B.id DESC
         LIMIT 1
      ) AS lon
    FROM 'myTable' A
    ORDER BY A.id DESC;
    

    Demo

        
    answered by 06.11.2017 в 22:26