简体   繁体   English

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

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

I need to update empty values in a table with values presents in another table.我需要用另一个表中的值更新表中的空值。 Shortly these values are ids of groups, and I need to pass these groupids in all events of my users to recognize in which group the user held this event.很快,这些值就是组的 id,我需要在我的用户的所有事件中传递这些 groupid,以识别用户在哪个组中举行了这个事件。 Example:例子:

Inputs:输入:

Table A:表 A:

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

Table B:表 B:

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

Desired Output:所需的 Output:

Table A:表 A:

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

isn't it just a simple join?不就是一个简单的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