sqlite 分组后获取每组的第一条记录

xiaoxiao2025-05-23  47

有表如下 CREATE TABLE test ( id INTEGER PRIMARY KEY ,groupid INTEGER DEFAULT 0 ,t INTEGER DEFAULT 0 ,x INTEGER DEFAULT 0 ,y INTEGER DEFAULT 0 )

内容 REPLACE INTO test (id,groupid, t ,x,y) select 1,1,100,8,8 union all select 2,1,80 ,7,7 union all select 3,1,50,6,6 union all select 4,2,100,5,5

要求结果,找到每个groupid中t最大的记录的完整展示(即id 1,4) 1 1 100 8 8 4 2 100 5 5

现在使用 select * from test group by groupid order by t desc 无法达到要求

---------------------------------------->>>

在查询中增加一个函数的使用,即可达到了分组后想要的数据结果如下:

select min(id) minId,a.* from test a group by groupid;

 

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-5030595.html

最新回复(0)