mysql时间比较处理,时间范围处理

建表语句:

DROP TABLE IF EXISTS `exam`;
CREATE TABLE `exam`  (
  `exam_id` int(11) NOT NULL AUTO_INCREMENT,
  `exam_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '试卷名',
  `exam_start_date` datetime(0) DEFAULT NULL COMMENT '考试开始时间',
  `exam_last_time` bigint(20) DEFAULT NULL COMMENT '考试持续的时间,单位为分钟',
  `reviewer_id` int(11) DEFAULT NULL COMMENT '审批老师的id',
  `create_by` bigint(20) DEFAULT NULL COMMENT '创建者',
  `create_date` datetime(0) DEFAULT NULL,
  `update_by` bigint(20) DEFAULT NULL,
  `update_date` datetime(0) DEFAULT NULL,
  `score` decimal(10, 0) DEFAULT NULL COMMENT '试卷总分',
  PRIMARY KEY (`exam_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 20 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '试卷表' ROW_FORMAT = Dynamic;




INSERT INTO `exam` VALUES (11, 'Java试卷1号', '2024-01-05 00:00:00', 60, 25, NULL, '2024-01-05 00:00:00', NULL, '2022-10-26 21:27:22', 0);
INSERT INTO `exam` VALUES (12, '测试试卷2号', '2024-01-05 00:00:00', 60, 26, NULL, '2024-01-05 00:00:00', NULL, NULL, 6);
INSERT INTO `exam` VALUES (13, '测试试卷3', '2024-01-05 00:00:00', 60, 26, NULL, '2024-01-05 00:00:00', NULL, NULL, 319);
INSERT INTO `exam` VALUES (14, 'java考试 测试1卷', '2024-01-05 00:00:00', 60, 25, NULL, '2024-01-05 00:00:00', NULL, NULL, NULL);
INSERT INTO `exam` VALUES (15, 'java考试1', '2024-01-05 00:00:00', 90, 25, NULL, '2024-01-05 00:00:00', NULL, NULL, NULL);
INSERT INTO `exam` VALUES (16, 'javaceshi', '2024-01-05 00:00:00', 90, 26, NULL, '2024-01-05 00:00:00', NULL, NULL, 11);
INSERT INTO `exam` VALUES (19, 'web', '2024-01-08 15:26:08', 60, 25, NULL, '2024-01-08 15:26:36', NULL, NULL, 0);

SET FOREIGN_KEY_CHECKS = 1;

具体写法:

select
    exam_id, exam_name, exam_start_date, exam_last_time, create_by, create_date, update_by,
    update_date,reviewer_id,score
from exam where 1=1 and
    date_format(create_date,'%y%m%d') >= date_format('2024-01-03','%y%m%d')
    and
    date_format(create_date,'%y%m%d') <= date_format('2024-01-08','%y%m%d');



select * from exam where create_date>'2024-01-04 00:00:00' and create_date<'2024-01-08 00:00:00';

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