在日期之间标记为假期

时间:2018-03-06 14:09:59

标签: mysql sql

所以我有这个DTR表。我想使用具有开始日期和结束日期的假期来标记日期(添加一个标记为假日的列),如图2所示。有没有人有解决方法如何实现这一目标?

CREATE TABLE IF NOT EXISTS `payroll_dtr` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `employmentRecordId` int(11) DEFAULT NULL,
  `employeeId` varchar(50) DEFAULT NULL,
  `date` date NOT NULL,
  `timeinAM` time DEFAULT NULL,
  `timeoutAM` time DEFAULT NULL,
  `timeinPM` time DEFAULT NULL,
  `timeoutPM` time DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `employmentRecordId` (`employmentRecordId`),
  KEY `employeeId` (`employeeId`)
) ENGINE=InnoDB AUTO_INCREMENT=73 DEFAULT CHARSET=latin1;

-- Dumping data for table db_hrmis.payroll_dtr: ~71 rows (approximately)
/*!40000 ALTER TABLE `payroll_dtr` DISABLE KEYS */;
INSERT INTO `payroll_dtr` (`employmentRecordId`, `employeeId`, `date`, `timeinAM`, `timeoutAM`, `timeinPM`, `timeoutPM`) VALUES
    (10, 'DRB201822210', '2018-02-01', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-02', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-05', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-06', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-07', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-08', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-09', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-12', '08:34:44', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-13', '06:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-14', '08:35:00', '12:05:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-15', '08:30:00', '12:00:00', '13:05:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-19', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-20', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-21', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-22', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-23', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-26', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-27', '08:30:00', '12:00:00', '13:00:00', '17:30:00'),
    (10, 'DRB201822210', '2018-02-28', '08:30:00', '12:00:00', '13:00:00', '17:30:00');

图片1。 enter image description here

图2

CREATE TABLE IF NOT EXISTS `inf_calendar_of_events` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) NOT NULL,
  `start` date DEFAULT NULL,
  `end` date DEFAULT NULL,
  `timeStart` time DEFAULT NULL,
  `timeEnd` time DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

-- Dumping data for table db_hrmis.inf_calendar_of_events: ~6 rows (approximately)
/*!40000 ALTER TABLE `inf_calendar_of_events` DISABLE KEYS */;
INSERT INTO `inf_calendar_of_events` (`title`, `start`, `end`, `timeStart`, `timeEnd`) VALUES
    ('Test', '2018-02-16', '2018-02-20', NULL, NULL);

enter image description here

所需的输出,但不是让isHoliday = 1,而是希望它是Holiday table中的日期。

enter image description here

感谢您的帮助。对不起,如果我没有提供完整的数据。

1 个答案:

答案 0 :(得分:2)

看起来像一个简单的连接

select d.id,dte,
         case when dte between date_from and date_to then 'x'
         else ''
         end holiday
from dates d
cross join holiday_table
where entity_id = 3 and dte between '2018-02-14' and '2018-02-21'

+------+------------+---------+
| id   | dte        | holiday |
+------+------------+---------+
| 6620 | 2018-02-14 |         |
| 6621 | 2018-02-15 | x       |
| 6622 | 2018-02-16 | x       |
| 6623 | 2018-02-17 | x       |
| 6624 | 2018-02-18 | x       |
| 6625 | 2018-02-19 | x       |
| 6626 | 2018-02-20 | x       |
| 6627 | 2018-02-21 |         |
+------+------------+---------+