MYSQL 时间轴数据 获取同一天数据的前3条

  创建表数据

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;

 

posted @ 2019-07-16 14:33 一只怀揣IT梦想的蜗牛 阅读(...) 评论(...) 编辑 收藏