我有一张这样的 table -
+----+---------------------+------------+---------+--------------------+
| 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 按编号顺序分组,但它不起作用。
最佳答案
你可以这样使用:
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
关于Mysql查询以获取输入的最后状态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12695352/