用户状态的SQL查询

用户状态的SQL查询,sql,postgresql,join,Sql,Postgresql,Join,我不确定如何编写Postgres视图来选择正确的记录。 我有两张桌子: 用户选项卡 user_id email absence_id user_id date_from date_to 及 缺勤选项卡 user_id email absence_id user_id date_from date_to 缺勤时,选项卡表示用户不可用时的日期范围(在工作中;)。 我想列出在某个日期可用的所有用户,这样他们就不会在该日期的“缺勤”选项卡中出现 感谢您的建议。此查询的输出将是2016-08-29

我不确定如何编写Postgres视图来选择正确的记录。 我有两张桌子:

用户选项卡

user_id
email
absence_id
user_id
date_from
date_to

缺勤选项卡

user_id
email
absence_id
user_id
date_from
date_to
缺勤时,选项卡表示用户不可用时的日期范围(在工作中;)。 我想列出在某个日期可用的所有用户,这样他们就不会在该日期的“缺勤”选项卡中出现


感谢您的建议。

此查询的输出将是
2016-08-29
中的
缺勤选项卡中不存在的用户,这意味着,正如您所说,他们在当天可用

select *
from user_tab u
where not exists (
  select 1
  from absence_tab a
  where u.user_id = a.user_id
    and '2016-08-29' between date_from and date_to
  )

此查询的输出将是
2016-08-29
中的
缺勤选项卡
中不存在的用户,这意味着,正如您所说,他们在当天可用

select *
from user_tab u
where not exists (
  select 1
  from absence_tab a
  where u.user_id = a.user_id
    and '2016-08-29' between date_from and date_to
  )

好的,这个查询将起作用,但我想为此创建一个视图。因此,如果我想将日期作为查询参数(WHERE)传递,那么还应该选择列
deposition\u from
deposition\u to
)。你知道吗?你可以用一个集合返回函数来代替创建一个视图。一个视图需要完全不同的代码,恐怕效率不高,因为它需要更多的数据处理。因此,如果我想将日期作为查询参数(WHERE)传递,那么还应该选择列
deposition\u from
deposition\u to
)。你知道吗?你可以用一个集合返回函数来代替创建一个视图。一个视图将需要完全不同的代码,而且我担心它的效率不高,因为它需要更多的数据处理。