mysql 获取当天零点和当天最后时间

        要进行数据统计,统计前一天的数据量

时间处理:

当天零点

 SELECT DATE(NOW()) AS today_zero_time;

当天最后时间:

 SELECT DATE_ADD(ADDDATE(DATE(NOW()), 1), INTERVAL -1 SECOND) AS today_last_time;

 昨天零点

SELECT  ADDDATE(DATE(NOW()), -1) AS yesterday_last_time;

昨天最后时间:

SELECT  DATE_ADD(DATE(NOW()), INTERVAL -1 SECOND) AS yesterday_last_time;

 指定日期零点

SELECT DATE(STR_TO_DATE("2024-08-08 12:13:14", '%Y-%m-%d %H:%i:%s')) AS appoint_zero_time;

指定日期最后时间:

SELECT DATE_ADD(ADDDATE(DATE(STR_TO_DATE("2024-08-08 12:13:14", '%Y-%m-%d %H:%i:%s')), 1), INTERVAL -1 SECOND) AS appoint_last_time;

数据准备:

表格:

CREATE TABLE `cust_test_letter` (
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '标识',  
  `letter_info` VARCHAR(1000) DEFAULT NULL COMMENT '来信内容',
  `letter_date` DATE  DEFAULT NULL COMMENT '来信日期', 
  `letter_time` DATETIME DEFAULT NOW() COMMENT '来信日期时间',
  PRIMARY KEY (`id`) 
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='测试来信';

数据:

insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('1','需要整改','2024-07-31','2024-07-31 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('2','需要整改','2024-08-05','2024-08-05 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('3','需要整改','2024-08-06','2024-08-06 10:08:44');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('4','需要整改','2024-08-01','2024-08-01 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('5','需要整改','2024-08-09','2024-08-09 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('6','需要整改','2024-08-12','2024-08-12 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('7','需要整改','2024-08-09','2024-08-07 08:57:33');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('8','需要整改','2024-08-10','2024-08-07 08:59:36');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('9','需要整改','2024-08-07','2024-08-07 08:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('10','需要整改','2024-08-06','2024-08-06 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('11','需要整改','2024-08-07','2024-08-07 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('12','需要整改','2024-08-01','2024-08-01 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('13','需要整改','2024-08-07','2024-08-07 13:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('14','需要整改','2024-08-07','2024-08-07 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('15','需要整改','2024-08-08','2024-08-08 00:00:00');
insert into cust_test_letter (id, letter_info, letter_date, letter_time) values('16','需要整改','2024-08-08','2024-08-08 09:09:37');

查询处理:

查询昨天的数据:

大于等于小于
 SELECT * FROM cust_test_letter  ctl  
 WHERE ctl.letter_date >=  ADDDATE(DATE(NOW()), -1) 
AND  ctl.letter_date < DATE(NOW()) ;

between 
 SELECT * FROM cust_test_letter  ctl  
 WHERE ctl.letter_date BETWEEN  ADDDATE(DATE(NOW()), -1) 
AND DATE_ADD(DATE(NOW()), INTERVAL -1 SECOND);

感觉用大于等于和小于的方式更为直观

总结:

        mysql 查询数据的时候,要注意时间的临界点,避免数量对应不上。

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值