S
S
sane4e4ek2017-09-28 12:01:48
PHP
sane4e4ek, 2017-09-28 12:01:48

Comparing dates in Mysql?

Hello.
I understand that the question can be trivial and I look at it from the wrong side, but my head is already boiling and I don’t think at all.
Voobshchemto an essence of a question:
There is a certain table in a DB, it has 2 fields date_start and date_end - the beginning of holiday and the end of holiday, it is set in YYYY-MM-DD format.
these fields will be available to all employees.
The next task is to display employees whose vacation dates overlap.
for example, employee number 1 date_start = 2017-05-20 and date_end 2017-06-05. Employee #2 has date_start = 2017-06-01 and date_end 2017-06-20.
how to make a query in MySQL to show that these time intervals intersect

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2017-09-28
@sane4e4ek

SELECT * 
  FROM `table` AS `t1`
  JOIN `table` AS `t2` ON `t1`.`user_id` < `t2`.`user_id` 
    AND `t1`.`date_start` <= `t2`.`date_end` AND `t2`.`date_start` <= `t1`.`date_end`

D
Denis Holub, 2017-09-28
@denman1985

select t1.*, t2.*
from table t1 join table t2 on (((t1.d_beg >= t2.d_beg and t1.d_beg <= t2.d_end) or (t1.d_end >= t2.d_beg and t1.d_end <= t2.d_end)) and t1.id_sotr <> t2.id_sotr)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question