Detailed explanation of the method of comparing dates in MySQL

Detailed explanation of the method of comparing dates in MySQL

If there is a table product with a field add_time, whose data type is datetime, someone might write the SQL like this:

The code is as follows

select * from product where add_time = '2013-01-12'

For this statement, if the format you store is YY-mm-dd, then it's OK. If the format you store is: 2013-01-12 23:23:56, then you are in trouble. This is the part where you can use the DATE() function to return the date, so this SQL should be processed as follows:

The code is as follows

select * from product where Date(add_time) = '2013-01-12'

Another one, what if you want to query the products added in January 2013?

The code is as follows

select * from product where date(add_time) between '2013-01-01' and '2013-01-31'

You can also write:

select * from product where Year(add_time) = 2013 and Month(add_time) = 1

Now you should know the role of MySQL date functions in dealing with date comparison problems, right?

Its date_col value is within the last 30 days:

The code is as follows

mysql> SELECT something FROM table 
 WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30;
DAYOFWEEK(date)

Returns the day of the week index of date (1=Sunday, 2=Monday, ... 7=Saturday). These index values ​​correspond to the ODBC standard.

The code is as follows

mysql> select DAYOFWEEK('1998-02-03'); 
 -> 3 
WEEKDAY(date)

Returns the day of the week index of date (0=Monday, 1=Tuesday, ... 6=Sunday).

The code is as follows

mysql> select WEEKDAY('1997-10-04 22:23:00'); 
 -> 5 
 mysql> select WEEKDAY('1997-11-05'); 
 -> 2 
DAYOFMONTH(date)

Returns the day of the month in date, in the range 1 to 31.

The code is as follows

mysql> select DAYOFMONTH('1998-02-03'); 
 -> 3 
DAYOFYEAR(date)

Returns the day of the year for date, in the range 1 to 366.

The code is as follows

mysql> select DAYOFYEAR('1998-02-03'); 
 -> 34 
MONTH(date)

Returns the month of date, ranging from 1 to 12.

The code is as follows

mysql> select MONTH('1998-02-03'); 
 -> 2 
DAYNAME(date)

Returns the day of the week of date.

The code is as follows

mysql> select DAYNAME("1998-02-05"); 
 -> 'Thursday' 
MONTHNAME(date)

Returns the name of the month in date.

The code is as follows

mysql> select MONTHNAME("1998-02-05"); 
 -> 'February' 
QUARTER(date)

Returns the quarter of the year for date, in the range 1 to 4.

The code is as follows

mysql> select QUARTER('98-04-01'); 
 -> 2

If you want to have a more in-depth and systematic study of MySQL, you can refer to the classic book "MySQL King's Road to Promotion".

Summarize

The above is the method of comparing dates in MySQL 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!

You may also be interested in:
  • Multiple ways to calculate age by birthday in MySQL
  • Summary of MySQL date data type and time type usage
  • MySQL date functions and date conversion and formatting functions
  • MySQL date and time format conversion implementation statement
  • Detailed explanation of mysql to get the current date and format
  • MySql uses DATE_FORMAT to intercept the date value of the DateTime field
  • Mysql date time DATE_FORMAT (date, format)
  • Detailed explanation of MySql date query statement
  • Mysql example of converting birth date into age and grouping and counting the number of people

<<:  A brief discussion of 3 new features worth noting in TypeScript 3.7

>>:  Detailed explanation of the best configuration for Nginx to improve security and performance

Recommend

Implementation idea of ​​left alignment of the last row of flex box layout

Using flex layout, if it is a nine-square grid, i...

Methods and problems encountered in installing mariadb in centos under mysql

Delete the previously installed mariadb 1. Use rp...

js to achieve a simple lottery function

This article shares the specific code of js to im...

The first step in getting started with MySQL database is to create a table

Create a database Right click - Create a new data...

How to use cursor triggers in MySQL

cursor The set of rows returned by the select que...

Specific steps for Vue browser to return monitoring

Preface When sharing a page, you hope to click th...

A commonplace technique for implementing triangles using CSS (multiple methods)

In some interview experiences, you can often see ...

Some questions about hyperlinks

<br />I am very happy to participate in this...

How to use Javascript to generate smooth curves

Table of contents Preface Introduction to Bezier ...

The process of using vxe-table to make editable tables in vue

There is a table in the project that needs to be ...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

Specific use of CSS front-end page rendering optimization attribute will-change

Preface When scroll events such as scroll and res...

Summary of methods for cleaning Mysql general_log

Method 1: SET GLOBAL general_log = 'OFF';...