In Postgres, suppose I have two tables.
在Postgres中,假设我有两张桌子。
Table A:
表A:
ID Gender
1
1
2
2
3
3
Table B:
表B:
ID Gender
1 F
2 M
3 F
How could I insert the gender values from Table B to Table A and get new table A like this: Table A:
如何将表B中的性别值插入表A并获取如下的新表A:表A:
ID Gender
1 F
1 F
2 M
2 M
3 F
3 F
I tried commands like:
我尝试过如下命令:
insert into a(gender) select gender from b
where a.id=b.id;
and it gives me error info:
它给了我错误信息:
there is an entry for table "a", but it cannot be referenced from this part of the query.
表“a”有一个条目,但不能从查询的这一部分引用它。
2 个解决方案
#1
3
Please try this:
请试试这个:
UPDATE TABLE "A" Set "Gender" = "B"."Gender"
FROM "B"
WHERE "A"."ID" = "B"."ID";
Hopefully it will help you.
希望它会对你有所帮助。
#2
0
Have you tried any joins
? (i.e. inner or outer)
你尝试过任何连接吗? (即内部或外部)
SELECT a.ID, b.Gender
FROM tableA a INNER JOIN tableB b
ON b.ID = a.ID;
However, this would be considered as separated result set or you could do the insert operation
但是,这将被视为分离结果集,或者您可以执行插入操作
#1
3
Please try this:
请试试这个:
UPDATE TABLE "A" Set "Gender" = "B"."Gender"
FROM "B"
WHERE "A"."ID" = "B"."ID";
Hopefully it will help you.
希望它会对你有所帮助。
#2
0
Have you tried any joins
? (i.e. inner or outer)
你尝试过任何连接吗? (即内部或外部)
SELECT a.ID, b.Gender
FROM tableA a INNER JOIN tableB b
ON b.ID = a.ID;
However, this would be considered as separated result set or you could do the insert operation
但是,这将被视为分离结果集,或者您可以执行插入操作