Boundary and range description of between in mysql

Boundary and range description of between in mysql

mysql between boundary range

The range of between is inclusive of the boundary values ​​on both sides

Eg: id between 3 and 7 is equivalent to id >=3 and id<=7

The range of not between does not include the boundary value

Eg: id not between 3 and 7 is equivalent to id < 3 or id>7

SELECT * FROM `test` where id BETWEEN 3 and 7;
Equivalent to SELECT * FROM `test` where id>=3 and id<=7;
-----------------------------------------------------------
SELECT * FROM `test` where id NOT BETWEEN 3 and 7;
Equivalent to SELECT * FROM `test` where id<3 or id>7;

Note the issue of mysql between date boundaries

Boundary issues:

mysql, between start date AND end date includes the start date and excludes the end date

For example:

BETWEEN '2018-01-22' AND '2018-01-30'

The start date is 2018-01-22 00:00:00.0 and ends at 2018-01-29 23:59:59.59

CREATE_DATE in the table is varchar(21) DEFAULT NULL COMMENT 'time',

The value stored in CREATE_DATE is: year-month-day hour:minute:second:0 For example: 2018-01-29 23:45:35.0

SELECT * FROM Test a WHERE a.CREATE_DATE BETWEEN '2018-01-22' AND '2018-01-30'   
ORDER BY a.CREATE_DATE desc 

SELECT * FROM TABEL a WHERE a.CREATE_DATE BETWEEN '2018-01-22' AND '2018-01-30'   
ORDER BY a.CREATE_DATE desc
    2018-01-29 23:45:35.0 20180129
    2018-01-29 23:45:33.0 20180129
    2018-01-29 00:10:58.0 20180129
    2018-01-29 00:10:45.0 20180129
    2018-01-28 23:42:23.0 20180128
    2018-01-28 23:39:39.0 20180128
SELECT * FROM TABEL a WHERE a.CREATE_DATE BETWEEN '2018-01-22' AND '2018-01-29'   
ORDER BY a.CREATE_DATE desc
    2018-01-28 23:42:23.0 20180128
    2018-01-28 23:39:39.0 20180128
    2018-01-28 00:13:22.0 20180128
    2018-01-28 00:13:19.0 20180128
    2018-01-27 23:23:02.0 20180127
    2018-01-22 00:09:59.0 20180122
    2018-01-22 00:09:56.0 20180122
    2018-01-22 00:01:53.0 20180122

Other problems encountered:

Another table test2 has a field for saving time: `REPORTTIME` varchar(45) DEFAULT NULL,

The value stored in this field is:

Example 1:

select * from bips_hpd_helpdesk a WHERE str_to_date(from_unixtime(a.REPORTTIME,'%Y-%m-%d'),'%Y-%m-%d') 
BETWEEN '2018-01-16' AND '2018-01-27' ORDER BY from_unixtime(a.REPORTTIME,'%Y-%m-%d') DESC ;

Result 1:

From the results, we can see that the data for the 27th was obtained. It may be that the processing time does not include hours, minutes, and seconds.

Example 2:

select * from bips_hpd_helpdesk a WHERE str_to_date(from_unixtime(a.REPORTTIME,'%Y-%m-%d'),'%Y-%m-%d') 
BETWEEN str_to_date('2018-01-16','%Y-%m-%d') AND str_to_date('2018-01-27','%Y-%m-%d')

Result 2:

Found the problem: When converting millisecond values ​​to time, I found that the millisecond values ​​saved here do not save the hours, minutes, and seconds:

from_unixtime(a.REPORTTIME,'%Y-%m-%d') AS reportTime,a.REPORTTIME,  
             str_to_date(from_unixtime(a.REPORTTIME,'%Y-%m-%d'),'%Y-%m-%d %h:%i:%s') AS reportTime22
        FROM test a WHERE str_to_date(from_unixtime(a.REPORTTIME,'%Y-%m-%d'),'%Y-%m-%d %h:%i:%s') 
        BETWEEN str_to_date('2018-01-16','%Y-%m-%d %h:%i:%s') AND str_to_date('2018-01-27 %h:%i:%s','%Y-%m-%d')
     #subdate(curdate(),date_format(curdate(),'%w')-1) AND subdate(curdate(),date_format(curdate(),'%w')-8)
        ORDER BY from_unixtime(a.REPORTTIME,'%Y-%m-%d') DESC;

Viewed time value:

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the usage of the BETWEEN clause in MySQL
  • Tutorial on using BETWEEN and IN in MySQL's WHERE clause
  • Detailed explanation of the method of comparing dates in MySQL

<<:  CSS implements the web component function of sliding the message panel

>>:  Do you know how to use Vue to take screenshots of web pages?

Recommend

Detailed explanation of nginx shared memory mechanism

Nginx's shared memory is one of the main reas...

Implementation of mounting NFS shared directory in Docker container

Previously, https://www.jb51.net/article/205922.h...

Introduction to installing and configuring JDK under CentOS system

Table of contents Preface Check and uninstall Ope...

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

Solutions for high traffic websites

First: First, confirm whether the server hardware ...

An example of installing MySQL on Linux and configuring external network access

Configuration steps 1. Check whether DNS is confi...

Exploring the use of percentage values ​​in the background-position property

How background-position affects the display of ba...

Detailed tutorial on installing Docker and docker-compose suite on Windows

Table of contents Introduction Download and insta...

Detailed explanation of common for loop in JavaScript statements

There are many loop statements in JavaScript, inc...

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

Sharing tips on using Frameset to center the widescreen

Copy code The code is as follows: <frameset co...

A brief discussion on JS prototype and prototype chain

Table of contents 1. Prototype 2. Prototype point...

How to use Node.js to determine whether a png image has transparent pixels

background PNG images take up more storage space ...