2

With MySQL, I need to find rows where two values are between two columns.

For example, I'm creating a schedule from '15:00' to '18:00', but the query must verify if any row has an period in use. If there is a record from '14:00' to '18:30', I need to return it to validate.

I tried something like, but not works:

select * from availabilities where (('15:00' or '18:00') between start_hour and end_hour)

Any idea?

Salman A
  • 262,204
  • 82
  • 430
  • 521
Eduardo
  • 508
  • 6
  • 18

2 Answers2

3

This is basically a "test if range overlaps another range" question. The following query should work as long as start time is less than end time:

SELECT *
FROM availabilities
WHERE '18:00' > start_hour AND end_hour > '15:00'
Salman A
  • 262,204
  • 82
  • 430
  • 521
2

You can use TIME() function to extract the time part of the datatime/timestamp column

Eg.

SELECT * FROM Table
WHERE TIME('timeStampCol') BETWEEN '12:00:00' AND '18:00:00'
Ripon
  • 141
  • 1
  • 8
  • http://stackoverflow.com/questions/6973844/select-records-by-time-in-interval-between-120000-and-180000-on-every-day – Ripon Nov 16 '16 at 18:26