我在sql select
中有条件我希望在checktime >= date and checktime <= date
时选择表中的数据,但在the condition is checktime >= date and checktime <= date
我的数据来自表格中的Y-m-d 06:15:00 until Y-m-d 07:45:00
:
userid name checktime
195807311982032005 re1 2017-12-28 07:13:02
196001132006041007 re2 2017-12-28 07:14:37
196004201992031005 op6 2017-12-28 06:53:08
196005011986032014 re3 2017-12-28 07:01:24
196007311985032006 re4 2017-12-28 07:01:23
然后是表格中的数据Y-m-d 07:45:00 until Y-m-d 23:45:00
:
userid name checktime
195807311982032005 re1 2017-12-28 07:55:02
196001132006041007 re2 2017-12-28 09:14:37
196004201992031005 op6 2017-12-28 10:53:08
在当前和晚期(名称op6)
找到了相同的数据
示例和this my query to show late person
:
select distinct on (co.userid) co.userid, ui.name
from checkinout co join
userinfo ui
on ui.userid = co.userid
where co.checktime >= '2017-12-28 07:45:00' and
co.checktime <= '2017-12-28 13:45:00' AND NOT EXISTS
(SELECT co.userid from checkinout WHERE co.checktime >= '2017-12-28 06:15:00'
and co.checktime <='2017-12-28 07:45:00');
当我运行null时,我只想在某些条件下show all data in checkinout table
but it's not showing again in present query
(在括号中选择)
答案 0 :(得分:0)
您的结果为null,因为您在where语句中自相矛盾。 如果您要加入co.userid,以匹配NOT EXISTS检查时间范围内的检查时间范围,则表示它存在。
您正在寻找在此范围内办理登机手续的用户
WHERE co.checktime >= '2017-12-28 06:15:00' and co.checktime <= '2017-12-28 07:45:00'
之前的时间范围包含在NOT EXISTS功能中评估的选项中。因此,那些userId将永远存在。
WHERE co.checktime > '2017-12-28 06:15:00' and co.checktime <='2017-12-28 23:15:00'
正在运行
NOT EXISTS (SELECT co.userid from checkinout WHERE co.checktime > '2017-12-28 06:15:00' and co.checktime <='2017-12-28 23:15:00');
在最后&#34;和&#34;永远都是假的,不会给你的选择带来任何结果。
希望这有帮助,我理解你的问题!
答案 1 :(得分:0)
你表达的逻辑是这种形式:
select co.userid, ui.name
from checkinout co join
userinfo ui
on ui.userid = co.userid
group by
from ch
having sum( (co.checktime >= '2017-12-28 06:15:00' and
co.checktime <= '2017-12-28 07:45:00'
)::int ) > 0 and
sum( (co.checktime > '2017-12-28 06:15:00' and
co.checktime <= '2017-12-28 23:15:00'
)::int) = 0;
然而,日期在条件上看起来并不合适。