Skip to content
Advertisement

How to get current and previous ids of current id in one query(mysql)

My table is like this:

employee(empid, ename,job);

The current empid is for example 10, I want the previous and next empids from the database.

The next and previous values may not be 11 and 9 respectively, I need to get it from the database only in a single query. Could any body please help me with this?

Advertisement

Answer

ex. empid = 4

select * from employee
where empid = (select max(empid) from employee where empid < 4)
   OR empid = (select min(empid) from employee where empid > 4)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement