[英]Redshift table update with join
I have 3 table t1, t2 and t3.我有 3 个表 t1、t2 和 t3。
t1 has 2 column-> id1, val1 t1 有 2 列-> id1, val1
t2 -> id2, val2
t3 -> id3, val3
If id1=id2 and id2 = id3
then I need to update val1 ad val3.然后我需要更新 val1 和 val3。 But I have repeating id1 and each should have same val3但是我有重复的 id1 并且每个都应该有相同的 val3
I am using我正在使用
update t1
inner join t2 on t1.id1 = t2.id2
inner join t3 on t2.id2 = t3.id3
set t1.val1 = t3.val3
;
But not able to do this.但无法做到这一点。
The correct syntax is:正确的语法是:
UPDATE table_name SET column = { expression | UPDATE table_name SET column = {表达式| DEFAULT } [,...]默认 } [,...]
[ FROM fromlist ] [来自列表]
[ WHERE condition ] [ WHERE条件]
So your UPDATE
statement should look as follows:因此,您的UPDATE
语句应如下所示:
update t1 set val1 = val3
from t2 inner join t3 on t2.id2 = t3.id3
where t1.id1 = t2.id2
;
See the Redshift documentation and their comprehensive UPDATE
examples .请参阅Redshift 文档及其全面的UPDATE
示例。
I needed to get values from the other table, t2.val3, on Redshift.我需要从 Redshift 上的另一个表 t2.val3 中获取值。 I used the following我使用了以下
update t1
set val1 = t2.val3
from t2 join t1 t on t.id = t2.id;
I have to re-name t1.我必须重命名 t1。 Otherwise Redshift complains.否则 Redshift 会抱怨。
Per @jie solution, but expanded a bit and w/o the distraction of a table alias as a table name (heh heh):每个@jie 解决方案,但扩展了一点,没有表别名作为表名的干扰(呵呵):
update
my_counts
set
my_count_delta = t1.my_count - t2.my_count
from
my_counts t1 join
my_counts t2 on
t1.group_id = t2.group_id and
t2.count_dt = ( t1.count_dt - interval '1 day' )
where
t1.count_error is null
A few notes:一些注意事项:
( t1.count_dt - interval '1 day' )
:heart: pg style sql 如何使日期加/减如此简单:( ( t1.count_dt - interval '1 day' )
LEFT JOIN
i was confounded that this would not run with a left join...the error message was very clear requiring "balanced join" or was it "even join" ...so i went back to the @jie version of JOIN
and found that the query was incredibly fast .作为LEFT JOIN
的狂热爱好者,我很困惑这不会与左连接一起运行......错误消息非常清楚需要“平衡连接”还是“偶数连接” ......所以我回到了@ jie 版本的JOIN
,发现查询速度非常快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.