SQL 从另一个表的字段更新一个表的字段 [英] SQL update fields of one table from fields of another one

查看:29
本文介绍了SQL 从另一个表的字段更新一个表的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子:

A [ID, column1, column2, column3]
B [ID, column1, column2, column3, column4]

A 将始终是 B 的子集(意味着 A 的所有列也在 B 中).

A will always be subset of B (meaning all columns of A are also in B).

我想用 A 中所有 A<列的数据更新 B 中具有特定 ID 的记录/代码>.此 ID 存在于 AB 中.

I want to update a record with a specific ID in B with their data from A for all columns of A. This ID exists both in A and B.

是否有 UPDATE 语法或任何其他方法可以在不指定列名的情况下执行此操作,只需说 设置 A 的所有列"?

Is there an UPDATE syntax or any other way to do that without specifying the column names, just saying "set all columns of A"?

我使用的是 PostgreSQL,因此也接受特定的非标准命令(但是,不是首选).

I'm using PostgreSQL, so a specific non-standard command is also accepted (however, not preferred).

推荐答案

可以使用非标准的FROM 子句.

You can use the non-standard FROM clause.

UPDATE b
SET column1 = a.column1,
  column2 = a.column2,
  column3 = a.column3
FROM a
WHERE a.id = b.id
AND b.id = 1

这篇关于SQL 从另一个表的字段更新一个表的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆