MySQL date processing function example analysis

MySQL date processing function example analysis

This article mainly introduces the example analysis of MySQL date processing functions. The example code is introduced in great detail in this article, which has a certain reference value for everyone's study or work. Friends in need can refer to it.

First, create a table for the experiment

drop table if exists t_student;

create table t_student(
  id int primary key auto_increment,
  name varchar(20) not null comment 'Name',
  birthday date comment 'birthday'
)Engine=InnoDB default charset utf8;


insert into t_student values(null,'tom','1992-02-03');
insert into t_student values(null,'jerry','1993-02-06');
insert into t_student values(null,'hank','1993-03-05');
insert into t_student values(null,'xiaoming',now());

The date type is the type that records the exact date of MySQL.

now() Function

Get the current time

year() , month(), dayofmonth()

The above three functions are used to extract the year, month, and day from a date or time.

For example, if you want to get students whose birthdays are in February

select * from t_student where month(birthday) = 2;

monthname() Function

Output the English word for the month

select monthname(birthday) from t_student;

timestampdiff() Function

Compare the difference between two dates

Example: The age of the student

select timestampdiff(year,birthday ,now()) as age from t_student;

The first parameter of the timestampdiff function is the unit of the calculation result: year, month, day, etc.

to_days()

Convert date to days

Calculates the number of days between two times, which is the same as timestampdiff(day, arg1, arg2).

Query students whose birthdays are less than 60 days from the current date

select * from t_student where (to_days(now()) - to_days(birthday)) < 60;

date_add and date_sub

Calculate another date based on a date. date_add is for addition and date_sub is for subtraction.

select date_add('1970-1-1', interval 10 year); # 1970 plus 10 years

select date_sub('1970-1-1', interval 10 year); #1970 minus 10 years

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL calculates the number of days, months, and years between two dates
  • Solve the problem when setting the date to 0000-00-00 00:00:00 in MySQL 8.0.13
  • Detailed explanation of the method of comparing dates in MySQL
  • Common date comparison and calculation functions in MySQL
  • MySQL date functions and date conversion and formatting functions
  • mysql gets yesterday's date, today's date, tomorrow's date, and the time of the previous hour and the next hour
  • Detailed explanation of MySQL date string timestamp conversion
  • Detailed explanation of mysql to get the current date and format

<<:  Solution to the problem of IP loss caused by copying centos8 virtual machine under VMWARE

>>:  How to Install Oracle Java 14 on Ubuntu Linux

Recommend

Implementation of installing and uninstalling CUDA and CUDNN in Ubuntu

Table of contents Preface Install the graphics dr...

js method to delete a field in an object

This article mainly introduces the implementation...

Detailed explanation of meta tags and usage in html

I won’t waste any more time talking nonsense, let...

How to create LVM for XFS file system in Ubuntu

Preface lvm (Logical Volume Manager) logical volu...

MySQL 5.7.18 winx64 installation and configuration method graphic tutorial

The installation of compressed packages has chang...

How to match the size of text in web design: small text, big experience

With the rise of mobile terminals such as iPad, p...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

MySQL Series Database Design Three Paradigm Tutorial Examples

Table of contents 1. Knowledge description of the...

Implementation of ssh non-secret communication in linux

What is ssh Administrators can log in remotely to...

MySQL Community Server 5.7.19 Installation Guide (Detailed)

MySQL official website zip file download link htt...

Using vue3 to imitate the side message prompt effect of Apple system

Table of contents Animation Preview Other UI Libr...

In-depth analysis of the Identifier Case Sensitivity problem in MySQL

In MySQL, you may encounter the problem of case s...

React new version life cycle hook function and usage detailed explanation

Compared with the old life cycle Three hooks are ...

Calculation of percentage value when the css position property is absolute

When position is absolute, the percentage of its ...