MYSQL 时间轴数据 获取同一天数据的前3条
程序员文章站
2023-04-04 23:50:54
创建表数据 CREATE TABLE `praise_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `pic_id` varchar(64) DEFAULT NULL COMMENT '图片ID', `created_ti ......
创建表数据
create table `praise_info` (
`id` bigint(20) not null auto_increment comment 'id',
`pic_id` varchar(64) default null comment '图片id',
`created_time` datetime default current_timestamp comment '创建时间',
primary key (`id`),
key `pic_id` (`pic_id`) using btree
) engine=innodb auto_increment=3647 default charset=utf8 comment='图片表';
添加数据省略
时间轴前2条数据
select * from
(
select *, @num := if(@created_time = date_format(created_time, '%y-%m-%d'), @num := @num + 1, 1) as row_num,
@created_time := date_format(created_time, '%y-%m-%d') as axistime from praise_info
order by id desc
) as temp
where row_num < 3;
上一篇: iOS 为何使用runtime方法交换多次后却能按照交换顺序依次执行代码逻辑?
下一篇: 简易聊天室