根据其他两个表的比较更新列中的值
Update values in a column based on a comparison of two other tables
在 postgres 中,我有 3 个 table 由两个键连接:
键A连接tables 1和2,在table1中不唯一,在table2中唯一,在table3中不存在
键 B 加入 tables 2 和 3 在 table 1 中不存在并且在 tables 2 和 3 中是唯一的。
我希望更新 table 3 中的一列,该列应包含 table 1 中的记录计数(该列中的所有值均为 0)。
Table_1
+-----+
| Key |
+-----+
| A1 |
| A1 |
| A1 |
| A2 |
| A2 |
| A3 |
| A3 |
+-----+
Table_2
+-------+-------+
| Key_A | Key_B |
+-------+-------+
| A1 | B1 |
| A2 | B2 |
| A3 | B3 |
+-------+-------+
Table_3(期望的结果)
+-------+--------+
| Key_B | Count |
+-------+--------+
| B1 | 3 |
| B2 | 2 |
| B3 | 2 |
+-------+--------+
我被更新命令卡住了(对它们还不够熟悉),我得到了我需要的计数:
Select Table_3.Key_B, count(Table_1.*)
from Table_1
Join Table_2 on Table_1.Key_A = Table_2.Key_A
Join Table_3 on Table_2.Key_B = Table 3.Key_B
Group by 1
我只是不太确定如何用正确的计数更新 table 3 中的记录。我想我可能需要一个功能,但我不确定。这是在正确的轨道上吗?
Create or replace function my_funct
returns varchar as
$body$
Declare
r record
begin
select key_B from Table_3 into r;
update Table_3
set count = (
select count(*)
from table_1
Join Table_2 on Table_1.Key_A = Table_2.Key_A
Join Table_3 on Table_2.Key_B = Table 3.Key_B
Where key_B = r
);
end
$body$
您需要在子查询中使用 相关性 条件:
update Table_3
set count = (select count(*)
from table_1 t1 join
table_2 t2
on t1.Key_A = t2.Key_A
where t2.Key_B = Table_3.Key_B
);
Table_3
的引用来自外部查询。
实际上,假设 Table_1
和 Table_2
之间存在适当的外键关系并且 Table_1
没有重复,您甚至不需要 join
:
update Table_3
set count = (select count(*)
from table_2 t2
where t2.Key_B = Table_3.Key_B
);
查看问题后,我意识到情况并非如此,但我还是提供了解决方案。
避免昂贵的相关子查询:
update t3
set c = t.c
from (
select t2.b, count(*) as c
from t1 join t2 on t1.a = t2.a
group by 1
) t
where t3.b = t.b
;
table t3;
b | c
---+---
1 | 3
2 | 2
3 | 2
据我了解,table_2
中的key_B
是table_3
的外键,所以没有必要在from
子句中加入table_3
。
create table t2 (a int primary key, b int unique);
create table t1 (a int references t2);
create table t3 (b int unique references t2, c int);
insert into t2 (a, b) values (1,1),(2,2),(3,3);
insert into t1 (a) values (1),(1),(1),(2),(2),(3),(3);
insert into t3 (b) values (1),(2),(3);
UPDATE #table3 SET _Count = Cnt FROM (SELECT COUNT(*) Cnt,#table1.Key_A,Key_B KeyB FROM #table1 JOIN #table2 ON #table1.Key_A = #table2.Key_A GROUP BY #table1.Key_A,Key_B ) A WHERE KeyB = Key_B
在 postgres 中,我有 3 个 table 由两个键连接:
键A连接tables 1和2,在table1中不唯一,在table2中唯一,在table3中不存在
键 B 加入 tables 2 和 3 在 table 1 中不存在并且在 tables 2 和 3 中是唯一的。
我希望更新 table 3 中的一列,该列应包含 table 1 中的记录计数(该列中的所有值均为 0)。
Table_1
+-----+
| Key |
+-----+
| A1 |
| A1 |
| A1 |
| A2 |
| A2 |
| A3 |
| A3 |
+-----+
Table_2
+-------+-------+
| Key_A | Key_B |
+-------+-------+
| A1 | B1 |
| A2 | B2 |
| A3 | B3 |
+-------+-------+
Table_3(期望的结果)
+-------+--------+
| Key_B | Count |
+-------+--------+
| B1 | 3 |
| B2 | 2 |
| B3 | 2 |
+-------+--------+
我被更新命令卡住了(对它们还不够熟悉),我得到了我需要的计数:
Select Table_3.Key_B, count(Table_1.*)
from Table_1
Join Table_2 on Table_1.Key_A = Table_2.Key_A
Join Table_3 on Table_2.Key_B = Table 3.Key_B
Group by 1
我只是不太确定如何用正确的计数更新 table 3 中的记录。我想我可能需要一个功能,但我不确定。这是在正确的轨道上吗?
Create or replace function my_funct
returns varchar as
$body$
Declare
r record
begin
select key_B from Table_3 into r;
update Table_3
set count = (
select count(*)
from table_1
Join Table_2 on Table_1.Key_A = Table_2.Key_A
Join Table_3 on Table_2.Key_B = Table 3.Key_B
Where key_B = r
);
end
$body$
您需要在子查询中使用 相关性 条件:
update Table_3
set count = (select count(*)
from table_1 t1 join
table_2 t2
on t1.Key_A = t2.Key_A
where t2.Key_B = Table_3.Key_B
);
Table_3
的引用来自外部查询。
实际上,假设 Table_1
和 Table_2
之间存在适当的外键关系并且 Table_1
没有重复,您甚至不需要 join
:
update Table_3
set count = (select count(*)
from table_2 t2
where t2.Key_B = Table_3.Key_B
);
查看问题后,我意识到情况并非如此,但我还是提供了解决方案。
避免昂贵的相关子查询:
update t3
set c = t.c
from (
select t2.b, count(*) as c
from t1 join t2 on t1.a = t2.a
group by 1
) t
where t3.b = t.b
;
table t3;
b | c
---+---
1 | 3
2 | 2
3 | 2
据我了解,table_2
中的key_B
是table_3
的外键,所以没有必要在from
子句中加入table_3
。
create table t2 (a int primary key, b int unique);
create table t1 (a int references t2);
create table t3 (b int unique references t2, c int);
insert into t2 (a, b) values (1,1),(2,2),(3,3);
insert into t1 (a) values (1),(1),(1),(2),(2),(3),(3);
insert into t3 (b) values (1),(2),(3);
UPDATE #table3 SET _Count = Cnt FROM (SELECT COUNT(*) Cnt,#table1.Key_A,Key_B KeyB FROM #table1 JOIN #table2 ON #table1.Key_A = #table2.Key_A GROUP BY #table1.Key_A,Key_B ) A WHERE KeyB = Key_B