I'm building an application using a Postgres database, which involves comparing schedules. I'm using knex to connect it up to node; but if the solution is best served by a raw query, I can go that route as well.
I have a table called "Schedules"
which contains a "from_time"
and a "to_time"
.
I want to be able to give the db a "start_time"
and "end_time"
and find:
"start_time"
and "end_time" (ie, from_time <= start_time && end_time >= to_time
) "start_time"
and "end_time"
(ie, (start_time <= from_time && end_time > from_time) || (start_time < to_time && end_time >= from_time))
One possible solution I considered is simply storing the values as integers in the Unix epoch... which is my backup plan. However, since these really are time values, it's probably best to keep them in timestamp value format.
This one is for your first condition
knex.select('*').from('Schedules').where('from_time', '<=', start_time).where('to_time', '<=', end_time)
This one is for your second
knex.select('*').from('Schedules').where(function () {
this.where('from_time', '>=', start_time).where('from_time' ,'<', end_time))
}).orWhere(function () {
this.where('to_time', '>', start_time).where('from_time' ,'<=', end_time))
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.