mysql - 如何获取 MySQL 中两列范围之间的记录?
问题描述
这是我的下表,即machine_shifts
CREATE TABLE `machine_shifts` (
`date` date NOT NULL,
`shift_start_time` time DEFAULT NULL,
`shift_end_time` time DEFAULT NULL,
`shift` varchar(20) NOT NULL,
`updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`date`,`shift`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
数据是
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-01','00:00:00','06:00:00','C','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-01','06:00:00','13:00:00','A','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-01','13:00:00','24:00:00','B','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-02','00:00:00','06:00:00','C','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-02','06:00:00','13:00:00','A','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-02','13:00:00','24:00:00','B','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-03','00:00:00','06:00:00','C','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-03','06:00:00','13:00:00','A','2020-01-29 15:37:26');
insert into `machine_shifts` (`date`, `shift_start_time`, `shift_end_time`, `shift`, `updated_on`) values('2010-01-03','13:00:00','24:00:00','B','2020-01-29 15:37:26');
假设我有一台机器,它从2010:01:01时间07:01:00开始,到2010:01:03时间10:00:00结束。
我想查询上表以获取开始时间日期和结束时间日期之间的记录。
预期输出:
在标记线之间是预期的输出。
解决方案
尝试这个:
SELECT *
FROM machine_shifts
WHERE (STR_TO_DATE(concat(date, ' ', shift_start_time),'%Y-%c-%e %H:%i:%s' )
BETWEEN STR_TO_DATE('2010-01-01 07:01:00', '%Y-%c-%e %H:%i:%s')
AND STR_TO_DATE('2010-01-03 10:00:00', '%Y-%c-%e %H:%i:%s'))
AND (STR_TO_DATE(concat(date, ' ', shift_end_time),'%Y-%c-%e %H:%i:%s' )
BETWEEN STR_TO_DATE('2010-01-01 07:01:00', '%Y-%c-%e %H:%i:%s')
AND STR_TO_DATE('2010-01-03 10:00:00', '%Y-%c-%e %H:%i:%s'));
附加信息:
%Y
= 年(4 位)%c
= 月 (0-12)%e
= 月份中的某天 (0-31)%H
= 小时(00 到 23)%i
= 分钟(00 到 59)%s
= 秒(00 到 59)
或者,如果您希望满足任何条件 aer 的结果,请使用:
SELECT *
FROM machine_shifts
WHERE (STR_TO_DATE(concat(date, ' ', shift_start_time),'%Y-%c-%e %H:%i:%s' )
BETWEEN STR_TO_DATE('2010-01-01 07:01:00', '%Y-%c-%e %H:%i:%s')
AND STR_TO_DATE('2010-01-03 10:00:00', '%Y-%c-%e %H:%i:%s'))
OR (STR_TO_DATE(concat(date, ' ', shift_end_time),'%Y-%c-%e %H:%i:%s' )
BETWEEN STR_TO_DATE('2010-01-01 07:01:00', '%Y-%c-%e %H:%i:%s')
AND STR_TO_DATE('2010-01-03 10:00:00', '%Y-%c-%e %H:%i:%s'));
或没有额外 STR_TO_DATE 的版本:
SELECT *
FROM machine_shifts
WHERE (STR_TO_DATE(concat(date, ' ', shift_start_time),'%Y-%c-%e %H:%i:%s' )
BETWEEN '2010-01-01 07:01:00' AND '2010-01-03 10:00:00')
OR (STR_TO_DATE(concat(date, ' ', shift_end_time),'%Y-%c-%e %H:%i:%s' )
BETWEEN '2010-01-01 07:01:00' AND '2010-01-03 10:00:00');
推荐阅读
- spring-boot - JDBC 类型没有方言映射:在本机查询中选择“org.locationtech.jts.geom.Point”时出现 1111
- mysql - 如何将 PwnedPasswordsTop100k.txt 插入 MySQL
- php - 如果结果等于特定类别,Codeigniter 显示 TYPE 表中的 TYPE_LABLE
- vue.js - 在 `npm build` 的输出中添加源路径前缀
- mongodb - 单个 MongoDB 聚合管道中的总计数和带条件的字段计数
- python - 当字符串是变量时如何使用原始(r)字符串?
- vue.js - 如何在 vuex 的操作对象中使用异步和等待?
- sql - 如何解析json数据然后将其保存为sql server中的变量
- java - 你能在这段代码中找到无限循环吗?
- javascript - 当我点击使用 react.js 的选择时,如何显示三个参数?