Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在postgresql中,如何将一个表与另一个表的列相乘?_Python_Postgresql - Fatal编程技术网

Python 在postgresql中,如何将一个表与另一个表的列相乘?

Python 在postgresql中,如何将一个表与另一个表的列相乘?,python,postgresql,Python,Postgresql,我想将一个表a的一些列(除geom、gid、外键外)与另一个表B的列相乘。sum条件为: 其中A.gid=B.gid 我想做一些类似于以下伪代码的事情: FOR r IN SELECT [list of the column to multiply] DO A.r* B.sum WHERE A.gid = B.gid 如何在postgresql中实现这一点 我应该使用PL/pgSQL还是PL/Python 或者使用地理命令?可以在简单查询中将所选列乘以系数,例如: with table_a

我想将一个表
a
的一些列(除geom、gid、外键外)与另一个表
B的列相乘。sum
条件为:
其中A.gid=B.gid

我想做一些类似于以下伪代码的事情:

FOR r IN SELECT [list of the column to multiply] 
DO 
A.r* B.sum
WHERE A.gid = B.gid
如何在postgresql中实现这一点

我应该使用
PL/pgSQL
还是
PL/Python


或者使用
地理命令

可以在简单查询中将所选列乘以系数,例如:

with table_a(gid, col1, col2) as (
values
    (1, 10, 20),
    (2, 10, 30)
),
table_b(gid, sum) as (
values
    (1, 2),
    (2, 3)
)

select gid, col1* sum as col1, col2* sum as col2
from table_a
join table_b using(gid)

 gid | col1 | col2 
-----+------+------
   1 |   20 |   40
   2 |   30 |   90
(2 rows)

你想为每次乘法得到一个单独的值还是仅仅分组?我需要一个类似于a的表格,每个单元格乘以B.sum。我需要gid尊重表的连接。谢谢!最后,我在一个循环中使用了它,与
psycopg2
updatetable_a SET col1=col1*sum FROM table_b,其中table_a.foren_gid=table_b.gid