首页 > 解决方案 > 如何获取 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结束。

我想查询上表以获取开始时间日期和结束时间日期之间的记录。

预期输出:

在此处输入图像描述

在标记线之间是预期的输出。

标签: mysqlsqlselectbetween

解决方案


尝试这个:

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');

推荐阅读