简体   繁体   English

基于同一数据库中的另一个模式表列更新一个模式中的表

[英]Update table in one schema based on another schema table column in same database

I have two schemas in the same database.我在同一个数据库中有两个模式。 I want to update a table in one schema based on the table in another schema where the column value matches.我想根据列值匹配的另一个模式中的表更新一个模式中的表。 The structure of my database is as follows.我的数据库结构如下。

structure of database数据库结构

As an example, this is my table named "p" in 1st schema that is "public" and have column "id" and "name"例如,这是我在第一个模式中名为“p”的表,它是“public”并且有列“id”和“name”

id  name
3   Loral
1   Kim
2   Shawn

and this is the second table named "t" in the second schema that is "t_sc" and have column "id" and "name" but the names are different than table "p"这是第二个模式中名为“t”的第二个表“t_sc”并且具有列“id”和“name”但名称与表“p”不同

id  name
1   kylie
3   deny
2   tom

Now I want to update table "p" names according to table "t" names where the id matches.现在我想根据 id 匹配的表“t”名称更新表“p”名称。

I have tried the following query我试过以下查询

update p set p.Name = t_sc.t.Name WHERE p.ID = t_sc.t.ID

but got the following error但出现以下错误

ERROR: missing FROM-clause entry for table "t"错误:缺少表“t”的 FROM 子句条目

I have tried multiple other ways too but I am not able to get the desired result.我也尝试了多种其他方法,但无法获得所需的结果。 I am using Navicat for query and the database is Postgresql.我用Navicat查询,数据库是Postgresql。

If this is really MySQL, you need to call your schema(s) specifically.如果这真的是 MySQL,您需要专门调用您的架构。

You can thank this user for the example below:您可以感谢此用户提供以下示例:

Update a column on a table from a table on another schema MySql 从另一个模式上的表更新表上的列 MySql

update schema1.table1
inner join schema2.table2
on schema1.table1.IDColumn = schema2.table2.IDColumn
set schema1.table1.column1 = schema2.table2.column2

I have found the answer myself.我自己找到了答案。 So I thought maybe I should post it for future visitors.所以我想也许我应该为未来的访客发布它。

The query will be查询将是

UPDATE public.p a
SET name=b.name
FROM t_sc.t b
WHERE a.id=b.id

I was using the alias with column name p.Name of the Table which I want to update.我使用的别名是列名p.Name我要更新的表。 Also I was using the schema name at the wrong time t_sc.t.Name It was causing the error.此外,我在错误的时间使用了架构名称t_sc.t.Name它导致了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM