多选的 Pandas read_sql 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38646214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:41:36  来源:igfitidea点击:

Pandas read_sql query with multiple selects

pythonsqlsql-serverpython-3.xpandas

提问by Austin B

Can read_sql query handle a sql script with multiple select statements?

read_sql 查询可以处理具有多个选择语句的 sql 脚本吗?

I have a MSSQL query that is performing different tasks, but I don't want to have to write an individual query for each case. I would like to write just the one query and pull in the multiple tables.

我有一个执行不同任务的 MSSQL 查询,但我不想为每种情况编写单独的查询。我只想编写一个查询并拉入多个表。

I want the multiple queries in the same script because the queries are related, and it making updating the script easier.

我希望在同一个脚本中进行多个查询,因为这些查询是相关的,这使得更新脚本更容易。

For example:

例如:

SELECT ColumnX_1, ColumnX_2, ColumnX_3

FROM Table_X
INNER JOIN (Etc etc...)

----------------------
SELECT ColumnY_1, ColumnY_2, ColumnY_3

FROM Table_Y
INNER JOIN (Etc etc...)

Which leads to two separate query results.

这会导致两个单独的查询结果。

The subsequent python code is:

后续的python代码为:

scriptFile = open('.../SQL Queries/SQLScript.sql','r')
script = scriptFile.read()
engine = sqlalchemy.create_engine("mssql+pyodbc://UserName:PW!@Table")
connection = engine.connect()

df = pd.read_sql_query(script,connection)
connection.close()

Only the first table from the query is brought in.

只引入查询中的第一个表。

Is there anyway I can pull in both query results (maybe with a dictionary) that will prevent me from having to separate the query into multiple scripts.

无论如何,我是否可以同时提取两个查询结果(可能使用字典),这样我就不必将查询分成多个脚本。

采纳答案by Andy Hayden

You could do the following:

您可以执行以下操作:

queries = """
SELECT ColumnX_1, ColumnX_2, ColumnX_3

FROM Table_X
INNER JOIN (Etc etc...)
---
SELECT ColumnY_1, ColumnY_2, ColumnY_3

FROM Table_Y
INNER JOIN (Etc etc...)
""".split("---")

Now you can query each table and concat the result:

现在您可以查询每个表并连接结果:

df = pd.concat([pd.read_sql_query(q, connection) for q in queries])


Another option is to use UNION on the two results i.e. do the concat in SQL.

另一种选择是在两个结果上使用 UNION,即在 SQL 中进行连接。