php - 如何检索每个组中的最后一条记录

标签 php mysql group-by

我正在尝试使用 group by 从我的表中获取数据。使用 group by 可以正常工作,但我只需要获取每个组的最后插入的项目,但它不起作用。我的查询总是返回每组的第一项。

我的问题

SELECT id,type,userId,performDate,eventId FROM
`user_marker` where  `eventId`='842' and DATE_FORMAT(from_unixtime(performDate),'%Y%c%d')
=DATE_FORMAT(now(),'%Y%c%d') 
 and `visibility`='1'GROUP BY type ORDER BY id DESC

最佳答案

请试试

SELECT a.* FROM ( SELECT id,type,userId,performDate,eventId FROM 
`user_marker` where  `eventId`='842' and DATE_FORMAT(from_unixtime(performDate),'%Y%c%d')
=DATE_FORMAT(now(),'%Y%c%d') and `visibility`='1' ORDER BY id DESC ) as a GROUP BY a.type 

关于php - 如何检索每个组中的最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31109140/

相关文章:

php - 如何将MySQL数据库备份到SQL文件?

mysql - SQL错误:Can't DROP Index_name; check that column/key exists -- occurs multiple times

python - SQLAlchemy 错误 MySQL 服务器已消失

python - 使用 groupby 将分组数据转换为数据帧

c# - 在 IEnumerable<DataRow> 上分组

javascript - 在html页面上显示java变量

PHP - 伪随机数生成器?

mysql - 计算 datediff MySQL 时的差异

python - 使用 groupby 创建具有最大值的新列

php - 当另一个表保存特定值时,如何使用 MySQL 将某些内容插入到表中