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

About front-end JavaScript ES6 details

Table of contents 1. Introduction 1.1 Babel Trans...

Understanding render in Vue scaffolding

In the vue scaffolding, we can see that in the ne...

React+axios implements github search user function (sample code)

load Request Success Request failed Click cmd and...

Native javascript+CSS to achieve the effect of carousel

This article uses javascript+CSS to implement the...

Installing Windows Server 2008 operating system on a virtual machine

This article introduces the installation of Windo...

Two box models in web pages (W3C box model, IE box model)

There are two types of web page box models: 1: Sta...

mysql5.7.17.msi installation graphic tutorial

mysql-5.7.17.msi installation, follow the screens...

Detailed explanation of mysql trigger example

Table of contents What is a trigger Create a trig...

How to check if data exists before inserting in mysql

Business scenario: The visitor's visit status...

Analysis of product status in interactive design that cannot be ignored in design

In the process of product design, designers always...

Summary of WEBAPP development skills (notes for mobile website development)

1. To develop web responsively, the page must ada...

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...

Vue realizes web online chat function

This article example shares the specific code of ...