如何使用Petl连接来自不同数据库的两个表

时间:2019-06-22 11:52:28

标签: python petl

我正在使用petl python包对存储在SQL Server数据库中的表执行一些查询。我现在需要在不同数据库中的2个表之间做一个JOIN

据我所知,函数petl.fromdb仅接受一个连接[petl.fromdb(connection, query)]

有没有想法可以用petl做我想做的事情?如果没有,是否有任何包装能让我做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以分别从它们的数据库中读取两个表,然后使用petl.join()将它们联接:

import petl as etl

# Read both tables
table_a = etl.fromdb(connection_a, 'SELECT * FROM table_a')
table_b = etl.fromdb(connection_b, 'SELECT * FROM table_b')

# Join tables into new table
table_c = etl.join(table_a, table_b, key='id')

有关petl.join()的详细信息,请参见documentation