繁体   English   中英

如何使用 SQL(基于时间戳)中的其他值更新表列的值?

[英]How to update values of a column of a table with values from other in SQL (timestamp based)?

我需要用另一个表中的值更新表中的空值。 很快,这些值就是组的 id,我需要在我的用户的所有事件中传递这些 groupid,以识别用户在哪个组中举行了这个事件。 例子:

输入:

表 A:

事件 时间戳 群号 用户身份
创建 2021-03-09T09:58:17.198362522Z c98
更新 2021-03-09T09:59:17.198362522Z c98
创建 2021-03-09T09:46:17.198362522Z a32
更新 2021-03-09T10:20:17.198362522Z c98
更新 2021-03-09T10:22:17.198362522Z c98
创建 2021-03-09T09:58:17.198362522Z a32
创建 2021-03-09T10:40:17.198362522Z c98

表 B:

群号 时间戳 用户身份
f13 2021-03-09T09:58:17.198362522Z c98
f14 2021-03-09T10:15:17.198362522Z c98
广告8 2021-03-09T09:45:00.198362522Z a32
广告9 2021-03-09T09:58:17.198362522Z a32

所需的 Output:

表 A:

事件 时间戳 群号 用户身份
创建 2021-03-09T09:58:17.198362522Z f13 c98
更新 2021-03-09T09:59:17.198362522Z f13 c98
创建 2021-03-09T09:46:17.198362522Z 广告8 a32
更新 2021-03-09T10:20:17.198362522Z f14 c98
更新 2021-03-09T10:22:17.198362522Z f14 c98
创建 2021-03-09T09:59:17.198362522Z 广告9 a32
创建 2021-03-09T10:40:17.198362522Z f14 c98

不就是一个简单的join吗?

update table1 a
set groupid = (select groupid from table2 b
                where a.userid = b.userid
                and a.timestamp >= b.timestamp
                order by b.timestamp desc
                limit 1
              )
where a.groupid id null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM