lightdb/postgresql多表update更新示例
postgresql 示例 更新 update 多表 lightdb
2023-09-27 14:28:33 时间
在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
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单表是类似的。
相关文章
- Confluence 6 安装 PostgreSQL
- 阿里云RDS PostgreSQL OSS 外部表 - 并行写提速案例
- Postgresql 简单安装过程. Study From https://www.cnblogs.com/stulzq/p/7766409.html
- PostgreSQL中函数的三态
- 思考--PostgreSQL在与mysql的比较中稍微弱势项
- PostgreSQL索引思考
- PostgreSQL文档编译
- PostgreSQL日志号LSN和wal日志文件简记
- PostgreSQL.conf文件配置详解[转]
- PostgreSQL java读取bytes字段
- PostgreSQL学习手册(五) 函数和操作符
- postgresql-slony-I同步复制配置步骤
- postgresql/lightdb中分区的Constraint Exclusion详解
- lightdb/postgresql toast解析&页面物理组织
- lightdb/postgresql性能分析之ltcenter
- postgresql三种shutdown