1

我有一张这样的桌子-

+----+---------------------+------------+---------+--------------------+
| 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 |
+----+---------------------+------------+---------+--------------------+

我想获得每个号码的最后更新状态。我怎么得到这个?我尝试按 check_date desc 按编号顺序分组,但它不起作用。

4

2 回答 2

5

你可以使用这样的东西:

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

请参阅带有演示的 SQL Fiddle

于 2012-10-02T17:40:16.133 回答
0
select number, Status, Check_date from MYTable A
where check_date = (select max(Check_date) from MYTable where number=A.number)
于 2012-10-02T17:40:19.607 回答