views:

34

answers:

1
SELECT id ,MAX(status) AS status FROM Mail WHERE 
     status < (SELECT status FROM Mail WHERE id = 1000) 
     GROUP BY status ORDER BY status DESC LIMIT 1;

I am using this query to find the previous row of the current row 1000.Here i am getting NULL values if i use the status column which is not unique.where as it gives proper values if i use unique columns.

A: 

Try this:

SELECT m1.id, MAX(m1.status) AS 'status' FROM Mail m1, Mail m2
WHERE m1.status < m2.status AND m2.id = 1000
GROUP BY m1.status ORDER BY m1.status DESC LIMIT 1;
Amarghosh
Thanks , the problem is "Status" column is not unique so while sorting by status, it skips other rows having same status
shal
Then change `m1.status < m2.status` to `m1.status <= m2.status`
Amarghosh