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"
相关推荐
你的人类朋友24 分钟前
JWT的组成
后端
脑花儿43 分钟前
ABAP SMW0下载Excel模板并填充&&剪切板方式粘贴
java·前端·数据库
SELSL1 小时前
SQLite3的API调用实战例子
linux·数据库·c++·sqlite3·sqlite实战
洲覆1 小时前
Redis 核心数据类型:从命令、结构到实战应用
服务器·数据库·redis·缓存
傻啦嘿哟1 小时前
Python SQLite模块:轻量级数据库的实战指南
数据库·python·sqlite
维尔切1 小时前
HAProxy 负载均衡器
linux·运维·数据库·负载均衡
什么半岛铁盒1 小时前
C++项目:仿muduo库高并发服务器-------Channel模块实现
linux·服务器·数据库·c++·mysql·ubuntu
倔强的石头_1 小时前
【金仓数据库产品体验官】Windows 安装 KingbaseES V9R1C10 与 Oracle 兼容特性实战
数据库
北风朝向1 小时前
Spring Boot参数校验8大坑与生产级避坑指南
java·spring boot·后端·spring
望获linux2 小时前
【Linux基础知识系列:第一百四十篇】理解SELinux与系统安全
linux·运维·服务器·数据库·chrome·macos