当前位置: 首页 > news >正文

Postgresql使用update

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 解决问题
  • 一、关联表更新
    • 1.关联一张表
    • 2.关联多张表
  • 二、根据状态更新为不同的值


解决问题

通过多张关联表更新主表的字段,根据状态更新为不同的值。


一、关联表更新

1.关联一张表

更新 table1 表中 num 字段的值为 table2 表中的 sum 的值

update table1 a
set a.num = b.sum
from table2 b
where a.id = b.rel_id and b.type = '1';

2.关联多张表

更新 table1 表中 num 字段的值为 table2 和 table3 表中的 sum 字段的和

update table1 a
set a.num = b.sum + c.sum
from table2 b
left join table3 c on b.id = c.rel_id
where a.id = b.rel_id and c.type = '1';
update table1 a
set a.num = b.sum + c.sum
from table2 b, table3 c
where a.id = b.rel_id and b.id = c.rel_id and c.type = '1';

二、根据状态更新为不同的值

根据 table2 表中的 type 的值,更新 table1 表中 num 字段的值

update table1 a
set a.num = case when b.type = '1' then b.sum else b.sum + 1 end
from table2 b
where a.id = b.rel_id;

根据 table3 表中的 type 的值,更新 table1 表中 num 字段的值为 table2 表中的值

update table1 a
set a.num = case when c.type = '1' then b.interest_sum else b.interest_sum + 1 end
from (select sum(interest) as interest_sum from table2 group by country) b
left join table3 c on b.rel_id = c.id
where a.rel_id = b.id and a.type = '1';
http://www.lryc.cn/news/293249.html

相关文章:

  • Django视图函数技巧,从入门到实战
  • 部署实战--修改jar中的文件并重新打包成jar文件
  • RT-Thread线程管理(使用篇)
  • 【HarmonyOS】鸿蒙开发之ArkTs初步认识——第2.1章
  • 随手记:uni-app中使用iconfont彩色图标
  • 02-OpenFeign-微服务接入
  • 【前端工程化】环境搭建 nodejs npm
  • 在VM虚拟机搭建NFS服务器
  • springboot并mybatis入门启动
  • 什么是单例模式与饿汉式单例模式的区别是什么?
  • 【数据结构】认识数据结构 (通俗解释)
  • C语言——深入理解指针(1)
  • MySQL原理(五)事务
  • 算法学习——华为机考题库4(HJ26 - HJ30)
  • STM32学习笔记(三) —— GPIO点亮LED
  • gRPC使用详解
  • 海康威视有插件、无插件播放;webrtc直播;西瓜视频播放器;mpegts.js直播;flvjs直播
  • 测试工作(新入职)感悟
  • hivesql的基础知识点
  • Linux下的线程操作
  • 机器学习 | 如何利用集成学习提高机器学习的性能?
  • [Python] 什么是PCA降维技术以及scikit-learn中PCA类使用案例(图文教程,含详细代码)
  • npm 淘宝镜像正式到期,更新使用成功
  • python_蓝桥杯刷题记录_笔记_全AC代码_入门2
  • 备战蓝桥杯---数据结构与STL应用(入门4)
  • 2023_12蓝桥杯STEMA 考试 Scratch 中级试卷解析
  • 从编程中理解:大脑中的杏仁核
  • Maven dependency中的scope
  • 代码随想录算法训练营DAY11 | 栈与队列 (2)
  • 【Spring实战】33 Spring Boot3 集成 Nacos 配置中心