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"
相关推荐
等....2 小时前
Minio使用
数据库
GreenTea2 小时前
一文搞懂Harness Engineering与Meta-Harness
前端·人工智能·后端
win x3 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
迷枫7124 小时前
DM8 数据库安装实战:从零搭建达梦数据库环境(附全套工具链接)
数据库
我是大猴子4 小时前
Spring代理类为何依赖注入失效?
java·后端·spring
XDHCOM5 小时前
PostgreSQL 25001: active_sql_transaction 报错原因分析,故障修复步骤详解,远程处理解决方案
数据库·sql·postgresql
码事漫谈5 小时前
手把手带你部署本地模型,让你Token自由(小白专属)
前端·后端
卤炖阑尾炎5 小时前
PostgreSQL 日常运维全指南:从基础操作到备份恢复
运维·数据库·postgresql
码农BookSea6 小时前
ReAct:让大模型学会边想边做
后端·ai编程
码农BookSea6 小时前
10分钟掌握 JSON-RPC 协议,面试加分、设计不踩坑
后端