lightdb/postgresql多表update更新示例

在ansi sql规范中,是不允许update中包含join的,所以update多表实现通常采用子查询的方式实现,也就是oracle的形式。
在lightdb中,使用update from的形式

UPDATE
scholar
SET STATUS = s.status FROM student AS s WHERE scholar.id = s.id;
UPDATE sc_sp_o_c_score
  SET score = tmp.score
  FROM temp_weighted_scores_offers AS tmp
  WHERE tmp.fk_offer = fk_offer
    AND tmp.fk_offer IN (SELECT fk_offer FROM temp_offerids_with_score)
    AND fk_category = 1
    AND fk_searchprofile = 12345;

sql server和pg一样,支持update from。

mysql中的update from支持pg兼容、oracle兼容,以及“UPDATE table1 t1,table2,...,table n”形式来多表更新独有 三种语法。 因为第三种不好理解,所以不推荐。

mysql> UPDATE product p, product_price pp SET pp.price = p.price * 0.8 WHERE p.productid= pp.productId;
Query OK, 5 rows affected (0.02 sec)
Rows matched: 5  Changed: 5  Warnings: 0
mysql> UPDATE product p INNER JOIN product_price pp ON p.productid= pp.productid SET pp.price = p.price * 0.8;
Query OK, 5 rows affected (0.09 sec)
Rows matched: 5  Changed: 5  Warnings: 0
mysql> UPDATE product_price pp SET price=(SELECT price*0.8 FROM product WHERE productid = pp.productid);
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

oracle update子查询、内联视图或merge

复制代码
update t1 
   set t1.money = (select t2.money 
                     from t2 
                    where t2.name = t1.name
                   )
where exists (select 1 from t2 where t2.name = t1.name);

-- 不推荐,容易有歧义
update (
        select t1.money money1,t2.money money2 from t1,t2 where t1.name = t2.name
       ) t
   set t.money1 = t.money2;

----
merge into t1
           using (select t2.name,t2.money from t2) t
              on (t.name = t1.name)
    when matched then 
    update  set t1.money = t.money;
复制代码

  在内部实现上,update from的流程为,先update xxx from yyy join zzz进行join扁平化,标识xxx为target、xxx.aaa\bbb为targetentry(同时会带回tid便于直接更新),yyy和zzz为source。和merge以及update单表是类似的。

posted @   zhjh256  阅读(717)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2020-01-03 ORA-14452:试图创建,更改或删除正在使用的临时表中的索引
2020-01-03 经过 NGINX 加载js大文件加载不全,报206 (Partial Content)错误
2017-01-03 xinetd cpu 100%
2017-01-03 tomcat 504 gateway time-out
点击右上角即可分享
微信分享提示