PostgreSQL查询本周数据
我正在尝试获取本周已预订的所有约会我最初尝试了以下方法:PostgreSQL查询本周数据,sql,postgresql,Sql,Postgresql,我正在尝试获取本周已预订的所有约会我最初尝试了以下方法: select * from appointments where staff_id = 'id' and (to_timestamp(appointments.start_time)) > (current_date - interval '1 week') and (to_timestamp(appointments.start_time)) < (current_date + interval '1
select *
from appointments
where staff_id = 'id'
and (to_timestamp(appointments.start_time)) > (current_date - interval '1 week')
and (to_timestamp(appointments.start_time)) < (current_date + interval '1 week')
这只是选择所有等于+/-7天的约会,因此不是当前一周。这是我的下一次尝试。我知道它坏了,但我想它能说明问题
select *
from appointments
where staff_id = 'id'
and (to_timestamp(appointments.start_time)) > (current_date - interval concat(extract(dow from current_date), ' days'))
and (to_timestamp(appointments.start_time)) < (current_date + interval concat(7 - extract(dow from current_date), ' days'))
我将如何构建此查询 如果你同意Postgres对一周的定义,那么你可以:
select a.*
from appointments a
where staff_id = 'id' and
to_timestamp(appointments.start_time) >= date_trunc('week', current_date) and
to_timestamp(appointments.start_time) < date_trunc('week', current_date) + interval '1 week';