update语句使用表中的字段更新

前言

有个表中的字段,需要从一张表字段更新到另外一张表

update语句

数据库表

sql 复制代码
/*
 Navicat Premium Dump SQL

 Source Server         : 192.168.69.46
 Source Server Type    : PostgreSQL
 Source Server Version : 150003 (150003)
 Source Host           : 192.168.69.46:65432
 Source Catalog        : luban
 Source Schema         : public

 Target Server Type    : PostgreSQL
 Target Server Version : 150003 (150003)
 File Encoding         : 65001

 Date: 19/06/2025 11:17:19
*/


-- ----------------------------
-- Table structure for device_apply
-- ----------------------------
DROP TABLE IF EXISTS "public"."device_apply";
CREATE TABLE "public"."device_apply" (
  "id" int8 NOT NULL,
  "create_id" int8,
  "create_time" timestamp(6),
  "dr" int4,
  "last_modify_id" int8,
  "last_modify_time" timestamp(6),
  "tenant_id" int8,
  "version" int4,
  "apply_status" int4,
  "approve_id" int8,
  "end_time" timestamp(6),
  "reason" varchar(255) COLLATE "pg_catalog"."default",
  "start_time" timestamp(6),
  "remark" varchar(255) COLLATE "pg_catalog"."default",
  "org_id" int8
)
;

-- ----------------------------
-- Table structure for device_apply_bind
-- ----------------------------
DROP TABLE IF EXISTS "public"."device_apply_bind";
CREATE TABLE "public"."device_apply_bind" (
  "id" int8 NOT NULL,
  "create_id" int8,
  "create_time" timestamp(6),
  "dr" int4,
  "last_modify_id" int8,
  "last_modify_time" timestamp(6),
  "tenant_id" int8,
  "version" int4,
  "device_apply_id" int8,
  "device_id" int8,
  "org_id" int8
)
;

-- ----------------------------
-- Primary Key structure for table device_apply
-- ----------------------------
ALTER TABLE "public"."device_apply" ADD CONSTRAINT "device_apply_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table device_apply_bind
-- ----------------------------
ALTER TABLE "public"."device_apply_bind" ADD CONSTRAINT "device_apply_bind_pkey" PRIMARY KEY ("id");

update语句写法

vbnet 复制代码
update device_apply set org_id = hello.org_id

  from (select b.device_apply_id as id, d.org_id as org_id from device_core d INNER JOIN device_apply_bind b on d.id = b.device_id) hello
  where device_apply."id" = hello."id"
相关推荐
这个DBA有点耶23 分钟前
从库延迟的“二次放大”效应:一次大事务,拖垮整个读写分离
数据库·mysql·架构
LearnYard29 分钟前
自然语言驱动的数据图表生成:几款工具功能对比实践
数据库·百度·powerpoint
子兮曰32 分钟前
AI Agent 完整入门指南:从 LLM 到生产落地的 30+ 个核心概念
前端·后端·agent
一个有温度的技术博主1 小时前
MySQL 三大日志协同机制:redo log、undo log 与 binlog 的联合运作
数据库·mysql·oracle
渔夫正在掘金1 小时前
Cordis 插件热插拔能力深度解析
后端·node.js
不爱编程的小九九1 小时前
小九源码-springboot004-springboot智能阅读推荐系统
java·spring boot·后端
ltl1 小时前
ClickHouse 与 DuckDB 选型:不是同一类列存
数据库
码事漫谈1 小时前
为什么调试模式不崩溃,打包后却崩溃了?
后端
ltl1 小时前
RocksDB WAL 与 WriteBatch:持久化与原子批写
数据库
ltl1 小时前
流批一体与增量视图:Materialize、RisingWave 与 DBSP
数据库