我有这个表:选择多个时间行范围
CREATE TABLE logins(
id SERIAL NOT NULL PRIMARY KEY,
login_time TSRANGE NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id),
CONSTRAINT overlapping_timeslots EXCLUDE USING GIST (
user_id WITH =,
timeslot WITH &&
)
);
当login_time用户登录保存tsrange(login_time,logout_time)
。
现在我尝试寻找谁在登录的用户:
-- ('2013-12-31 16:40:05','2013-12-31 17:40:05')
-- ('2014-01-04 14:27:45','2014-01-04 17:30:56')
-- ('2014-01-05 14:59:55','2014-01-05 16:03:39')
-- ('2014-01-01 17:20:54','2014-01-01 22:50:57')
-- Not logged in at ('2013-12-31 18:40:05','2014-01-01 01:20:05')
我有这个疑问,但没有有用的结果
SELECT user_id FROM (
select * from logins
where user_id in(select user_id from timed_requests where timeslot && tsrange('2013-12-31 16:20:05','2013-12-31 17:40:05'))
and user_id in(select user_id from timed_requests where timeslot && tsrange('2014-01-04 14:30:45','2014-01-04 17:20:56'))
and user_id in(select user_id from timed_requests where timeslot && tsrange('2014-01-05 15:09:55','2014-01-05 16:00:39'))
and user_id in(select user_id from timed_requests where timeslot && tsrange('2014-01-01 17:20:54','2014-01-01 22:50:57')
and user_id not in(select user_id from timed_requests where timeslot && tsrange('2013-12-31 18:40:05','2014-01-01 01:20:05'))
) ss
GROUP BY user_id
order by user_id;
有谁知道我可以编写一个查询谁的搜索在3-4个给定时间点登录的用户。
表'timed_requests'进来了吗?你只引入了一个表'logins'。另外,你的Postgres版本? –