数据库教程:Mysql时间轴数据 获取同一天数据的前三条

创建表数据 create table `praise_info` ( `id` bigint(20) not null auto_increment comm

创建表数据

  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;

ps:下面看下mysql 生成 时间轴

  drop procedure if exists pro_dim_date;  tudou@gyyx  create procedure pro_dim_date(in bdate date,in edate date)  begin  declare var date default bdate;  declare evar date default date_add(edate,interval 1 day);  declare bweek date;  declare eweek date;  while var<evar do  set bweek = date_add(date_sub(var,interval 1 week),interval 1 day);  set eweek = date_sub(date_add(var,interval 1 week),interval 1 day);  insert into gyyx_report.dim_date  (  `date_id`,  `date_name`,  `date_of_month`,  `year_id`,  `year_name`,  `quarter_id`,  `quarter_name`,  `month_id`,  `month_name`,  `month_of_year_name`,  `month_of_year_id`,  `week_id`,  `week_name`,  `week_of_year_id`,  `week_of_year_name`,  `is_weekend`  )  values  (  date_format(var,'%y%m%d'),  date_format(var,'%y-%m-%d'),  dayofmonth(var),  year(var),  concat(year(var),'年'),  quarter(var),  concat(quarter(var),'季度'),  date_format(var,'%y%m'),  concat(year(var),'年',month(var),'月'),  concat(month(var),'月'),  month(var),  weekday(var),  case weekday(var) when 0 then '星期一' when 1 then '星期二' when 2 then '星期三' when 3 then '星期四' when 4 then '星期五' when 5 then '星期六' when 6 then '星期日' end,  weekofyear(var),  concat('第',weekofyear(var),'周(',month(bweek),'月',day(bweek),'日~',month(eweek),'月',day(eweek),'日'),  case when weekday(var)>4 then '是' else '否' end  );  set var=date_add(var,interval 1 day);  end while;  end

调用:

  call pro_dim_date('2005-01-01','2013-12-31')

结果:

  20131217  2013-12-17 17 2013  2013年  4  4季度 201312 2013年12月  12月 12 1  星期二 51 第51周(12月11日~12月23日 否  20131218  2013-12-18 18 2013  2013年  4  4季度 201312 2013年12月  12月 12 2  星期三 51 第51周(12月12日~12月24日 否  20131219  2013-12-19 19 2013  2013年  4  4季度 201312 2013年12月  12月 12 3  星期四 51 第51周(12月13日~12月25日 否  20131220  2013-12-20 20 2013  2013年  4  4季度 201312 2013年12月  12月 12 4  星期五 51 第51周(12月14日~12月26日 否  20131221  2013-12-21 21 2013  2013年  4  4季度 201312 2013年12月  12月 12 5  星期六 51 第51周(12月15日~12月27日 是  20131222  2013-12-22 22 2013  2013年  4  4季度 201312 2013年12月  12月 12 6  星期日 51 第51周(12月16日~12月28日 是  20131223  2013-12-23 23 2013  2013年  4  4季度 201312 2013年12月  12月 12 0  星期一 52 第52周(12月17日~12月29日 否  20131224  2013-12-24 24 2013  2013年  4  4季度 201312 2013年12月  12月 12 1  星期二 52 第52周(12月18日~12月30日 否  20131225  2013-12-25 25 2013  2013年  4  4季度 201312 2013年12月  12月 12 2  星期三 52 第52周(12月19日~12月31日 否  20131226  2013-12-26 26 2013  2013年  4  4季度 201312 2013年12月  12月 12 3  星期四 52 第52周(12月20日~1月1日  否  20131227  2013-12-27 27 2013  2013年  4  4季度 201312 2013年12月  12月 12 4  星期五 52 第52周(12月21日~1月2日  否  20131228  2013-12-28 28 2013  2013年  4  4季度 201312 2013年12月  12月 12 5  星期六 52 第52周(12月22日~1月3日  是  20131229  2013-12-29 29 2013  2013年  4  4季度 201312 2013年12月  12月 12 6  星期日 52 第52周(12月23日~1月4日  是

表结构:

  create table `dim_date` (    `date_id` int(11) not null comment '20110512',    `date_name` varchar(16) default null comment '2011-05-12',    `date_of_month` int(11) default null comment '12',    `year_id` int(11) default null comment '2011',    `year_name` varchar(16) default null comment '2011年',    `quarter_id` int(11) default null comment '2',    `quarter_name` varchar(16) default null comment '2季度',    `month_id` int(11) default null comment '5',    `month_name` varchar(16) default null comment '5月',    `month_of_year_name` varchar(16) default null comment '2011年5月',    `month_of_year_id` int(11) default null comment '201105',    `week_id` int(11) default null,    `week_name` varchar(16) default null,    `week_of_year_id` int(11) default null,    `week_of_year_name` varchar(32) default null,    `is_weekend` enum('否','是') default null comment '是否周末',    primary key (`date_id`),    key `ix_dim_date_date_name` (`date_name`),    key `ix_dim_date_month_id` (`month_id`),    key `ix_dim_date_year_id` (`year_id`),    key `ix_dim_date_quanter_id` (`quarter_id`),    key `ix_dim_date_week_of_year_id` (`week_of_year_id`,`week_of_year_name`)  ) engine=myisam default charset=latin1

总结

以上所述是小编给大家介绍的mysql时间轴数据 获取同一天数据的前三条,希望对大家有所帮助

需要了解更多数据库技术:Mysql时间轴数据 获取同一天数据的前三条,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/824436.html

(1)
上一篇 2021年9月17日
下一篇 2021年9月17日

精彩推荐