欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql中between的边界,范围说明

程序员文章站 2022-06-26 21:32:54
mysql between的边界范围between 的范围是包含两边的边界值eg: id between 3 and 7 等价与 id >=3 and id<=7not 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

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 结束日期 包含开始日期,不包含结束日期

例如:

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

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

mysql中between的边界,范围说明

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:

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:

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的边界,范围说明

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

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的边界,范围说明

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