我有带有上传日期字段的表格图像。如果同一天上传了 2 张或更多图片,我如何获取最新的图片。
1.我们检查小时、分钟或秒。
2.如果H:M:S相同,检查插入ID的序列。
ID | URL | create |
1 | 01.jpg | 2017-02-23 10:24:41 |<<same H:M:s
2 | 02.jpg | 2017-02-23 10:24:41 |<<same H:M:s
3 | 03.jpg | 2017-02-23 10:50:00 |<<same H
4 | 04.jpg | 2017-02-24 21:50:00 |<<others
5 | 05.jpg | 2017-03-28 17:50:00 |<<others
输出:我只想得到
3 | 03.jpg | 2017-02-23 10:50:00 |<< newer than 1, 2
4 | 04.jpg | 2017-02-24 21:50:00 |
5 | 05.jpg | 2017-03-28 17:50:00 |
最佳答案
要获取给定日期具有最大时间戳的所有行,请使用
select created,max(id) maxid
from (select t1.*,(select count(distinct created) from t
where created >=t1.created
and cast(created as date)=cast(t1.created as date)) rn
from t t1) x
where rn=1
如果在最新的时间戳上有关联,并且在这种情况下只需要最新的 id,则上述查询可以扩展为以下内容。
select y.maxid id,t.url,t.created
from (
select created,max(id) maxid
from (select t1.*,(select count(distinct created) from t
where created >=t1.created
and cast(created as date)=cast(t1.created as date)) rn
from t t1) x
where rn=1
group by created
) y
join t on t.id=y.maxid and t.created=y.created
关于php - SQL - 如何获取创建的同一日期的最新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40755126/