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 查询数据的时候,要注意时间的临界点,避免数量对应不上。

  

你可能感兴趣的:(mysql,mysql,数据库)