SQL: GROUP BY records and then get last record from each group? [duplicate]

*爱你&永不变心* 提交于 2019-12-01 11:16:10
SELECT a.* 
FROM test a 
WHERE a.attendence = 1 
AND NOT EXISTS (select 1 from test where name = a.name and id > a.id and attendence = 1)
GROUP BY name 

Might not even need the group by anymore.

SELECT MAX("id"), "name" FROM "test" WHERE "attendence" = 1 GROUP BY "name"
Alex M
SELECT Name, Max(ID) FROM table WHERE attendance = 1 GROUP BY Name

Use the following query:

SELECT * FROM `test` WHERE `attendence`=1 GROUP BY name ORDER BY `id` DESC LIMIT 1

That will only select the row that meets the criteria with the highest id.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!