【问题标题】:JOIN Multiple Table based on timestamp and another condition基于时间戳和另一个条件加入多个表
【发布时间】: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


【解决方案1】:

通过添加datetime 字段,您将获得可以比较加入的时间戳。所以你可以像下面这样写你的查询:

select 
t1.userid, t1.date_in, t1.check_in, t2.checktime, t3.alias
from process t1
inner join checkinout t2 on t2. checktime= date_in + check_in and t1.userid=t2.userid
inner join table3 t3 on t2.sn=t3.sn

DEMO

关于您在问题ERROR: more than one row returned by a subquery used as an expression 中提到的错误是由于您使用的加入条件。

【讨论】:

  • 谢谢您,先生。真的解决了。日期+时间可以是时间戳。我不知道。
  • t2.checktime= date_in + check_in 和 t1.userid=t2.userid 你知道我如何在代码点火器中使用它来处理这段代码吗?我已经尝试过这个: $this->db->join ('checkinout t2', '(t2.checktime = date_in + check_in) and (t1.userid=t2.userid)');但是,仍然没有像上面的查询那样得到预期的结果。
【解决方案2】:

您可以使用 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

如图所示here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多