【发布时间】:2021-01-26 13:42:47
【问题描述】:
我想加入 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
表 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;
他们都给出错误: 错误:用作表达式的子查询返回多行
预期结果:
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
有人可以帮我解决这个问题吗? 谢谢你的帮助。
【问题讨论】:
-
所有
checktime日期如何在您的预期输出中为2017-01-21。请解释一下。 -
噢,对不起。我错误地编辑了预期的结果。我的意思是它与 date_in 相同。感谢您的关注。
-
噢,对不起。我错误地编辑了预期的结果。我的意思是它与 date_in 相同。感谢您的关注。
标签: sql postgresql join timestamp