一:按照某字段排序(时间,总数等),获取第一条
select a.* FROM ( select * from ( select t.*,row_number() over(partition by t.id order by t.createtime desc) rn from table t ) c where rn = 1) a where a.id= ‘001‘;--加上where条件是查询一条,不加是查询多条
二:如果是获取时间最新一条数据
SELECT * FROM table WHERE (id,createtime) IN (SELECT id,MAX(createtime) createtime FROM table GROUP BY id);--获取时间最新的多条数据 SELECT * FROM table where a.id=‘018‘ and createtime=(SELECT MAX(createtime) FROM table where a.id=‘018‘);--获取一条时间最新的数据
原文地址:https://www.cnblogs.com/liujiale/p/10938341.html
时间: 2024-10-29 15:40:48