Get the last entries using GROUP BY

耗尽温柔 提交于 2019-12-30 10:59:15

问题


I'm having problem with GROUP BY. It returns the first entry it could find, but I would like it to return the last entry. Is that possible?

Here is my query (prepared query):

SELECT stamp_user, stamp_date, stamp_type
FROM rws_stamps
WHERE stamp_date >= ?
GROUP BY stamp_user
ORDER BY stamp_date DESC

My table looks like this:

What I want it to return is row 7 and 3, but i get 1 and 2.


回答1:


Try:

SELECT stamp_user, max(stamp_date), stamp_type
FROM rws_stamps
WHERE stamp_date >= ?
GROUP BY stamp_user, stamp_type


来源:https://stackoverflow.com/questions/3811750/get-the-last-entries-using-group-by

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