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"
相关推荐
mzlogin7 分钟前
Java|FreeMarker 复用 layout
java·后端·freemarker
007php0079 分钟前
Go 语言常用命令使用与总结
java·linux·服务器·前端·数据库·docker·容器
MrHuang96515 分钟前
【创建线程的四种方式】
后端
双向3318 分钟前
Python 多线程日志错乱:logging.Handler 的并发问题
后端
用户9924410315621 分钟前
从秒杀系统到Serverless:我在分布式架构优化路上踩过的那些坑
后端
AAA修煤气灶刘哥21 分钟前
别再懵注解!从 JDK 到 SpringBoot,这篇 “代码贴标签” 攻略超下饭
java·后端·代码规范
共享家952724 分钟前
MySQL-索引(下)
数据库·mysql
IT_陈寒31 分钟前
Vite 3.0 性能飞跃的5个关键优化点,让构建速度提升200%!
前端·人工智能·后端
fat house cat_1 小时前
【Spring底层分析】Spring AOP补充以及@Transactional注解的底层原理分析
java·后端·spring
浊尘1 小时前
SQL Server 查看备份计划
数据库·sqlserver