mysql中日期算法_mysql中的一种时间计算

1、背景

工人类(ProjectWorker)有一类型为date的进场时间(entrytime):

private Date entrytime;

2、前台页面传过来两个过滤时间,通过两个属性来接收:

privateString startDate;private String endDate;

3、mysql中查询

and a.entrytime>=#{startDate}

and a.entrytime<=#{endDate}

4、结果展示:

4.1、  如果entrytime = 2019-09-12 00:00:00 ,startDate="2019-09-12", endDate="2019-09-12",

SELECT

a.id,

a. NAME,

a.sex,

a.nation,

a.entrytime

FROM

t_project_worker a

WHERE

a.do_id = 7

AND a.entrytime >= '2019-09-12'

AND a.entrytime<= '2019-09-12'

5c152ac2a0d1104f81c39038ed119281.png

4.2、 如果entrytime = 2019-09-12 00:00:01 ,startDate="2019-09-12", endDate="2019-09-12",

6f29a457ed9de0e490e513de78fce5e9.png

5、修改SQL

SELECT

a.id,

a. NAME,

a.sex,

a.nation,

a.entrytime

FROM

t_project_worker a

WHERE

a.do_id = 7

AND a.entrytime >= '2019-09-12 00:00:00'

AND a.entrytime<= '2019-09-12 23:59:59'

或者:

SELECT

a.id,

a. NAME,

a.sex,

a.nation,

a.entrytime

FROM

t_project_worker a

WHERE

a.do_id = 7

AND DATE_FORMAT(a.entrytime,'%Y-%m-%d') >= '2019-09-12'

AND DATE_FORMAT(a.entrytime,'%Y-%m-%d')<= '2019-09-12'

6、结论:

mySQL中如果传入一个年月日字符串当作日期,则默认为该日期的零点零分零秒。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值