Sql 基于时间戳和其他条件连接多个表
我想加入3个表 哪一个表A在不同的字段中有日期和时间,需要首先进行组合,以便使用字段时间戳在表B上进行比较 表ASql 基于时间戳和其他条件连接多个表,sql,postgresql,join,timestamp,Sql,Postgresql,Join,Timestamp,我想加入3个表 哪一个表A在不同的字段中有日期和时间,需要首先进行组合,以便使用字段时间戳在表B上进行比较 表A userid | date_in | check_in --------+------------+---------- 145 | 2017-01-23 | 08:56:05 254 | 2017-01-24 | 08:56:54 202 | 2017-01-25 | 08:53:26 15 | 2017-01-26 | 08:47:40
userid | date_in | check_in
--------+------------+----------
145 | 2017-01-23 | 08:56:05
254 | 2017-01-24 | 08:56:54
202 | 2017-01-25 | 08:53:26
15 | 2017-01-26 | 08:47:40
表B
userid | checktime | sn
--------+---------------------+---------------
145 | 2017-01-23 08:56:05 | 0345135200184
254 | 2017-01-24 08:56:54 | 0345135200184
202 | 2017-01-25 08:53:26 | 0345135200184
15 | 2017-01-26 08:47:40 | 0345135200184
表3
sn | alias
---------------+-------------
0345135200184 | Alam Sutera
我试过这个:
select process.userid, process.date_in, process.check_in, checkinout.checktime, iclock.alias from process
inner join checkinout on checkinout.checktime=(select cast (date_in as timestamp(0)) + (check_in - time '00:00:00') checktime from process)
inner join iclock on checkinout.sn=iclock.sn
order by userid desc limit 10;
select checkinout.userid, checkinout.checktime, checkinout.sn from checkinout
inner join lateral
(select distinct userid, date_in, check_in from process where
date_in=(select checktime::date from checkinout) and
check_in=(select checktime::time from checkinout)
order by date_in asc limit 1)
process ON true;
我也试过:
select process.userid, process.date_in, process.check_in, checkinout.checktime, iclock.alias from process
inner join checkinout on checkinout.checktime=(select cast (date_in as timestamp(0)) + (check_in - time '00:00:00') checktime from process)
inner join iclock on checkinout.sn=iclock.sn
order by userid desc limit 10;
select checkinout.userid, checkinout.checktime, checkinout.sn from checkinout
inner join lateral
(select distinct userid, date_in, check_in from process where
date_in=(select checktime::date from checkinout) and
check_in=(select checktime::time from checkinout)
order by date_in asc limit 1)
process ON true;
它们都给出了错误:
错误:用作表达式的子查询返回多行
预期结果:
userid | date_in | check_in | checktime | alias
--------+------------+----------+--------------------+-------
145 | 2017-01-23 | 08:56:05 | 2017-01-21 09:49:04|Alam Sutera
254 | 2017-01-24 | 08:56:54 | 2017-01-21 07:57:05|Alam Sutera
202 | 2017-01-25 | 08:53:26 | 2017-01-21 14:27:00|Alam Sutera
15 | 2017-01-26 | 08:47:40 | 2017-01-21 06:30:34|Alam Sutera
有人能帮我解决这个问题吗?
谢谢您的帮助。通过添加
日期
和时间
字段,您将获得时间戳,您可以比较加入时间戳。因此,您可以按如下方式编写查询:
选择
t1.userid,t1.date\u-in,t1.check\u-in,t2.checktime,t3.alias
来自进程t1
t2上的内部联接签出t2。checktime=date\u-in+check\u-in和t1.userid=t2.userid
t2上的内部连接表3 t3.sn=t3.sn
关于您在问题中提到的错误
错误:用作表达式的子查询返回的多行是由于您使用的联接条件造成的。您可以使用to_Timestamp函数将单独的日期和时间列转换为时间戳,如下所示。这将使您能够根据需要加入TableA和TableB
select * from tableA a
join tableB b
on a.userid = b.userid
and to_timestamp(a.date_in || ' ' || a.check_in, 'YYYY-MM-DD HH24:MI:SS') = b.checktime
如图所示所有检查时间
日期将如何在您的预期输出中2017-01-21
。请解释一下。哦,对不起。我错误地编辑了预期结果。我的意思是和date_in一样。谢谢你注意到了。哦,对不起。我错误地编辑了预期结果。我的意思是和date_in一样。谢谢你注意到了。谢谢你,先生。这真的解决了。日期+时间可以是这样的时间戳。。我不知道。t2.checktime=date\u in+check\u in和t1.userid=t2.userid你知道我如何在代码点火器中使用它吗?我已经试过了:$this->db->join('checkinout t2','(t2.checktime=date\u in+check\u in)和(t1.userid=t2.userid');但是,仍然不能像上面的查询那样得到预期的结果。