【问题标题】:How to fetch records in between the ranges of two columns in MySQL?如何获取 MySQL 中两列范围之间的记录?
【发布时间】:2020-02-06 07:34:26
【问题描述】:

这是我的下表,即 ma​​chine_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

我想查询上表,得到开始日期和结束日期之间的记录。

预期输出:

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

【问题讨论】:

  • 输出会是什么?
  • @juergend 编辑了问题并提到了预期的输出。
  • @KishoreKumarKorada 问题还不够清楚。
  • @ArifulIslam 我已经编辑了我的问题。如果您能理解,请检查一下吗?

标签: mysql sql select between


【解决方案1】:

试试这个:

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)

或者,如果您希望在满足任何条件的情况下获得结果,请使用:

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

【讨论】:

  • 我已经用表格的定义更新了问题
  • 嗨@KishoreKumarKorada 很好!这应该可以工作。你试过了吗?问题是什么 ?我已经更新了我的查询以满足您的数据要求...列日期只有年月日,而 shift_start_time 有时间部分...
  • 班次从 2010 年 01 月 1 日的 07:01:00 开始。这属于班次 06:00:00 和 13:00:00 即 A 班次。我需要那一行以便进行一些操作。假设如果有那一行,那么我稍后会知道那个时间机器在那个班次中。即从早上 7 点到下午 1 点(班次结束时间)。让我知道您仍然需要更多说明。我准备好了。
  • 嗨@KishoreKumarKorada,谢谢,不需要,我已经更新了我的查询。
  • 快乐编程:)
【解决方案2】:
SELECT *
FROM machine_shifts
WHERE STR_TO_DATE(concat(date, ' ', shift_start_time),'%Y-%e-%c %H:%i:%s' ) >= '2010-01-01 07:01:00' 
AND STR_TO_DATE(concat(date, ' ', shift_end_time),'%Y-%e-%c %H:%i:%s' ) <= '2010-01-03 10:00:00';

还有一个您可以实施的建议 - 在 shift_start_timeshift_end_time 上使用 DATETIME 字段而不是 TIME 并删除 date 列。这将使您的查询更容易和更快。

【讨论】:

  • 它不工作。如您所见, shift_start_time 和 shift_end_time 是日期的一部分。您的查询只返回空结果集。
  • @KishoreKumarKorada 请立即查看。
  • 这使用相同的逻辑来连接和转换日期列和时间列作为我的答案,如果数据不正确(例如开始日期是 2010:01:04 00:00:01 和结束日期是 2010-01-01 00:00:00) 这将返回这一行...
【解决方案3】:

select * from machine_shifts where date between 2010-01-01 and 2010-01-03 and shift_start_time >= start_time and shift_end_time

【讨论】:

  • 仅供参考。取决于轮班时间。
猜你喜欢
  • 2016-09-21
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2023-02-08
  • 1970-01-01
相关资源
最近更新 更多