分组排序 SUBSTRING_INDEX--------GROUP_CONCAT


分组查询一定时间段的一组数据 并且需要获取最近的一条数据
使用
SUBSTRING_INDEX
GROUP_CONCAT  默认 , 链接  可以用SEPARATOR 进行字符定义
GROUP_CONCAT(created_at ORDER BY created_at DESC
SEPARATOR '-'
)  对应的截取字符串时 也要相应修改
GROUP是组,CONCAT合并多个字符串,它的功能是:将group by产生的同一个分组中的值连接起来,返回一个字符串结果  
SUBSTRING_INDEX  截取字符串有效位数
select count(uid) as num, id,eid,created_at,uid,
GROUP_CONCAT(created_at ORDER BY created_at DESC) AS created_at from ds_ask_person
where eid=:eid and created_at >= :start and created_at <= :end group by uid order by created_at desc limit :offset,:limit


select count(uid) as num, id,eid,created_at,uid,
SUBSTRING_INDEX(GROUP_CONCAT(created_at ORDER BY created_at DESC),',',1) AS created_at from ds_ask_person
where eid=:eid and created_at >= :start and created_at <= :end group by uid order by created_at desc limit :offset,:limit

 

可参考 https://blog.csdn.net/lvoelife/article/details/81781802

posted @ 2019-03-13 11:11  迷失在路上  阅读(817)  评论(0编辑  收藏  举报