欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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;