Tom Lima
Tom Lima

Reputation: 1056

Select a previous value in a mysql query

I am currently working with an application in PHP and Mysql and I am having a little trouble operating a query.

This is my modules table:

     +----+-----------+----------+
     | Id |   Title   | Position |
     +----+-----------+----------+
     |  1 | Module 1  |        1 |
     |  2 | Module 2  |        0 |
     |  3 | Module 3  |        2 |
     +----+-----------+----------+

In my php code I get the id of a module to execute the query, for example: $moduleId = 3;

What I need to do is select the line with a previous position.

My module id is 3, in this id my position is 2, so I need to select a previous position ... in this case, the Module 1. How can I run this query?

Upvotes: 2

Views: 226

Answers (1)

Kunal Kukreja
Kunal Kukreja

Reputation: 769

select * from modules where 
position < (select position from modules where id=?) 
order by position desc limit 1;

where ? is substituted by the moduleId

Upvotes: 2

Related Questions