【发布时间】:2023-04-04 19:27:03
【问题描述】:
我有两个要加入的表。一个是记录特定时间事件的日志表。另一个是会话表。我想知道哪个事件发生在哪个会话中。
事件表具有timestamp(0) without time zone 类型的timestamp 字段,会话表具有timestamp without time zone 类型的logintime 和logouttime(userid 是两个表中都可用的字段) .
我试过的SQL是这样的:
select S.sessionid from event E
left join session S
ON S.logintime =
(
select MAX(logintime) max
from session
where (
(logintime < E.timestamp and logouttime > E.timestamp) OR
(logintime < E.timestamp AND logouttime is null)
) and S.userid=E.userid
)
当用户注销时,会话正常,但是当logouttime 是NULL 时,加入失败并且我没有得到sessionid。当我手动执行所有子查询时,它们似乎会产生正确的结果。
后者的例子:
首先,我们获取随机事件记录。
select * from event limit 1;
timestamp | userid | ....
---------------------+--------+-----
2014-07-15 15:44:24 | 195 | ....
其次,我们取join子句,填写接收到的数据
select MAX(logintime) max from session
where ((logintime <'2014-07-15 15:44:24' and logouttime > '2014-07-15 15:44:24') OR
(logintime < '2014-07-15 15:44:24' AND logouttime is null)) and userid=195;
max
----------------------------
2014-07-15 12:18:03.648875
最后,我们采用与那个时间匹配的 sessionid。
select * from session where logintime='2014-07-15 12:18:03.648875';
sessionid | userid | logintime | logouttime
--------------------------------------+--------+----------------------------+------------
4e56b28a-0c09-11e4-af0d-5b9f3f52ff0d | 195 | 2014-07-15 12:18:03.648875 |
【问题讨论】:
-
PostgreSQL 版本? (您可以添加为标签)
标签: postgresql join postgresql-9.3