Mysql timeline data to obtain the first three data of the same day

Mysql timeline data to obtain the first three data of the same day

Create table data

CREATE TABLE `praise_info` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
 `pic_id` varchar(64) DEFAULT NULL COMMENT 'Picture ID',
 `created_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
PRIMARY KEY (`id`),
 KEY `pic_id` (`pic_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3647 DEFAULT CHARSET=utf8 COMMENT='Picture table';

Add data omission

The first 2 data on the timeline

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: Let's take a look at the MySQL generation timeline

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),'quarter'),
DATE_FORMAT(var,'%Y%m'),
CONCAT(YEAR(var),'year',MONTH(var),'month'),
CONCAT(MONTH(var),'Month'),
MONTH(var),
WEEKDAY(var),
CASE WEEKDAY(var) WHEN 0 THEN 'Monday' WHEN 1 THEN 'Tuesday' WHEN 2 THEN 'Wednesday' WHEN 3 THEN 'Thursday' WHEN 4 THEN 'Friday' WHEN 5 THEN 'Saturday' WHEN 6 THEN 'Sunday' END,
WEEKOFYEAR(var),
CONCAT('第',WEEKOFYEAR(var),'周(',MONTH(bweek),'月',DAY(bweek),'日~',MONTH(eweek),'月',DAY(eweek),'日'),
CASE WHEN WEEKDAY(var)>4 THEN 'Yes' ELSE 'No' END
);
SET var=DATE_ADD(var,INTERVAL 1 DAY);
END WHILE;
END

Call:

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

result:

: : : : : : : : : : : : : : :

Table structure:

CREATE TABLE `dim_date` (
&nbsp; `date_id` int(11) NOT NULL COMMENT '20110512',
&nbsp; `date_name` varchar(16) DEFAULT NULL COMMENT '2011-05-12',
&nbsp; `date_of_month` int(11) DEFAULT NULL COMMENT '12',
&nbsp; `year_id` int(11) DEFAULT NULL COMMENT '2011',
&nbsp; `year_name` varchar(16) DEFAULT NULL COMMENT '2011',
&nbsp; `quarter_id` int(11) DEFAULT NULL COMMENT '2',
&nbsp; `quarter_name` varchar(16) DEFAULT NULL COMMENT '2季',
&nbsp; `month_id` int(11) DEFAULT NULL COMMENT '5',
&nbsp; `month_name` varchar(16) DEFAULT NULL COMMENT 'May',
&nbsp; `month_of_year_name` varchar(16) DEFAULT NULL COMMENT 'May 2011',
&nbsp; `month_of_year_id` int(11) DEFAULT NULL COMMENT '201105',
&nbsp; `week_id` int(11) DEFAULT NULL,
&nbsp; `week_name` varchar(16) DEFAULT NULL,
&nbsp; `week_of_year_id` int(11) DEFAULT NULL,
&nbsp; `week_of_year_name` varchar(32) DEFAULT NULL,
&nbsp; `is_weekend` enum('No','Yes') DEFAULT NULL COMMENT 'Is it the weekend',
&nbsp; PRIMARY KEY (`date_id`),
&nbsp; KEY `ix_dim_date_date_name` (`date_name`),
&nbsp; KEY `ix_dim_date_month_id` (`month_id`),
&nbsp; KEY `ix_dim_date_year_id` (`year_id`),
&nbsp; KEY `ix_dim_date_quanter_id` (`quarter_id`),
&nbsp; KEY `ix_dim_date_week_of_year_id` (`week_of_year_id`,`week_of_year_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Summarize

The above are the first three items of Mysql timeline data for obtaining data on the same day that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • MySQL statement to get all dates or months in a specified time period (without setting stored procedures or adding tables)
  • How to use MySQL DATEDIFF function to get the time interval between two dates
  • Detailed explanation of MySQL to obtain statistical data for each day and each hour of a certain period of time
  • MySQL example of getting today and yesterday's 0:00 timestamp
  • mysql gets yesterday's date, today's date, tomorrow's date, and the time of the previous hour and the next hour
  • mysql obtains statistical data within a specified time period
  • How to get time in mysql

<<:  Vue virtual Dom to real Dom conversion

>>:  WeChat applet calculator example

Recommend

Native js implements a minesweeper game with custom difficulty

This article example shares the specific code of ...

MySQL green decompression version installation and configuration steps

Steps: 1. Install MySQL database 1. Download the ...

Analysis of two implementation methods for adding static routing in Linux

Command to add a route: 1.Route add route add -ne...

FastDFS and Nginx integration to achieve code analysis

FastDFS & Nginx Integration: The tracker is c...

Vue implements multiple selections in the bottom pop-up window

This article example shares the specific code of ...

Detailed explanation of writing and using Makefile under Linux

Table of contents Makefile Makefile naming and ru...

Explanation of several ways to run Tomcat under Linux

Starting and shutting down Tomcat under Linux In ...

Steps to create your own YUM repository

To put it simply, the IP of the virtual machine u...

js to implement collision detection

This article example shares the specific code of ...

Basic tutorial on using explain statement in MySQL

Table of contents 1. Overview 1. Explain statemen...

Ubuntu 20.04 firewall settings simple tutorial (novice)

Preface In today's increasingly convenient In...

Detailed explanation of JavaScript function this pointing problem

Table of contents 1. The direction of this in the...

Detailed explanation of transactions and indexes in MySQL database

Table of contents 1. Affairs: Four major characte...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...