先用MAX(time) 和 group by item_id 查询出不同的item_id对应的最大时间,然后再在外面连表查询,查询 表中 item_id 和login_time 时间 相等于刚才的查询记录的记录
具体语句如下
select a.* from reyo a join (select item_id,max(login_time) as time from reyo group by item_id) b on a.item_id = b.item_id and a.login_time = b.time
查询用户最后一条阅读记录:
条件:member_id=238
$num = DB_LIST_ROWS; //分页个数
$limit=($pagenum-1)*DB_LIST_ROWS;
$member_id=$data['member_id'];
$sql_cnt="
SELECT a.* FROM lqf_book_read_log a
JOIN (select book_id,max(create_time) as time from lqf_book_read_log
WHERE member_id='{$member_id}' group by book_id ) b
ON a.book_id = b.book_id and a.create_time = b.time
ORDER BY a.create_time desc
";
//统计当前条件所有总数
$count_list = Db::query($sql_cnt);
$count = count($count_list)
//查询当前页数据
$sql_list=$sql_cnt." LIMIT {$limit},{$num}";
$list = Db::query($sql_list);
$datalist = Bootstrap::make($list, $num, $pagenum, $count, false, ['path' => Bootstrap::getCurrentPath(), 'query' => request()->param()])->toArray();