服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - mysql中between的边界范围说明

mysql中between的边界范围说明

2021-08-16 18:08markix Mysql

这篇文章主要介绍了mysql中between的边界,范围说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

mysql between的边界范围

between 的范围是包含两边的边界值

eg: id between 3 and 7 等价与 id >=3 and id<=7

not between 的范围是不包含边界值

eg:id not between 3 and 7 等价与 id < 3 or id>7

?
1
2
3
4
5
select * from `test` where id between 3 and 7;
等价于 select * from `test` where id>=3 and id<=7;
-----------------------------------------------------------
select * from `test` where id not between 3 and 7;
等价于 select * from `test` where id<3 or id>7;

mysql between日期边界的问题留意

边界问题:

mysql, between 开始日期 and 结束日期 包含开始日期,不包含结束日期

例如:

?
1
between '2018-01-22' and  '2018-01-30'

开始日期从2018-01-22 00:00:00.0 开始, 到2018-01-29 23:59:59.59结束

表中的create_date 是varchar(21) default null comment '时间',

create_date中保存值是: 年-月-日 时:分:秒:0 例如: 2018-01-29 23:45:35.0

?
1
2
select from  test  a where  a.create_date between '2018-01-22' and  '2018-01-30'  
order by a.create_date desc

mysql中between的边界范围说明

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

遇到的其他问题:

遇到另外一张表 test2 有保存时间的字段: `reporttime` varchar(45) default null,

这个字段保存的值是:

mysql中between的边界范围说明

例子1:

?
1
2
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 ;

结果1:

mysql中between的边界范围说明

从结果中,可以看到取到了27号的数据,可能是处理的时间没有 小时,分钟,秒。

例子2:

?
1
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')

结果2:

mysql中between的边界范围说明

找到问题: 毫秒值转换为时间,发现这里保存的毫秒值,没有保存时分秒:

?
1
2
3
4
5
6
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 ;

查看到的时间值:

mysql中between的边界范围说明

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://markix.blog.csdn.net/article/details/80365585

延伸 · 阅读

精彩推荐