Q:

Mysql query to get last status value entered

I've a table like this - 
+----+---------------------+------------+---------+--------------------+
| id | check_date          | number     | status  | sid                |
+----+---------------------+------------+---------+--------------------+
|  5 | 2012-09-29 17:44:34 | 3366064235 | SUCCESS | 1348940709163284   |
|  6 | 2012-09-29 19:40:30 | 3366064235 | FAILED  | 12345678876543     |
|  7 | 2012-09-29 17:47:30 | 4466064235 | SUCCESS | 7895345678876543   |
|  8 | 2012-09-29 19:47:30 | 4466064235 | SUCCESS | 789876545678876543 |
+----+---------------------+------------+---------+--------------------+

I want to get last updated status of each number. How do I get this? I tried group by number order by check_date desc but it doesn't work.

A:

You can use something like this:
select t1.id, 
    t1.check_date,
    t1.number,
    t1.status
from yourtable t1
inner join
(
    select max(check_date) maxdate, number
    from yourtable
    group by number
) t2
    on t1.check_date = t2.maxdate
    and t1.number = t2.number

See SQL Fiddle with demo

