SQL如何获取当月最后一次存在数据的时间

例如5月有31天,但是数据只更新到28号,我就想获取到28日的数据,如何自动生成获取时间

表中还有其他月份吗,可以加两个条件,一个是你要查询的月份,另一个条件使用max查询最大日期即可

条件小于当前月最后一条 排序后取第一条数据 即可

select 
  *
from tb 
where date_format('2021-07-16 12:00:00','%Y-%M-%d') = date_format(create_time,'%Y-%M-%d')
order by create_time DESC 
limit 1

selece top 1 * from table_name t where MONTH(t.xxxdatetime)=5 order by t.xxxdatetime desc

查询年月就行了,比如转成字符串,或者
select * from TTTT where year(XXX)=2021 and month(xxx)=5 order by xxx desc limit 1
如果只要日期
select max(xxx) from TTTT where year(XXX)=2021 and month(xxx)=5

可参考:
https://blog.csdn.net/ivan2015/article/details/46315431?utm_source=blogxgwz2