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.
As an example, this is my table named "p" in 1st schema that is "public" and have column "id" and "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"
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.
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"
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.
2
Answers
I have found the answer myself. So I thought maybe I should post it for future visitors.
The query will be
I was using the alias with column name
p.Name
of the Table which I want to update. Also I was using the schema name at the wrong timet_sc.t.Name
It was causing the error.If this is really MySQL, you need to call your schema(s) specifically.
You can thank this user for the example below:
Update a column on a table from a table on another schema MySql