首页 > 解决方案 > 如何在python中将一个查询输出用于其他查询?

问题描述

我正在尝试将一个查询输出用于另一个。但没有得到正确的结果。你能帮我怎么做吗?

例子:

query1 = "select distinct lower(tablename) as tablename from medaff.imedical_metadata where object_type = 'View'"

上述查询的输出是:

tablename
vw_mdcl_insght
vw_fbms_interactions

我想在其他查询中使用上述输出。像这样的东西-

query2 = "select * from medaff.imedical_business_metadata where objectname in ('vw_mdcl_insght', 'vw_fbms_interactions')"

如何在python中完成这部分?

我正在使用下面的代码来运行查询:

conn = redshift_conn()

with conn.cursor() as cur:
      query1 = "select distinct lower(tablename) as tablename from medaff.imedical_metadata where object_type = 'View'"
      cur.execute(sql_query)
      result = cur.fetchall()
      print(result)
      conn.commit()

      query2 = "select * from medaff.imedical_business_metadata where objectname in ('vw_mdcl_insght', 'vw_fbms_interactions')"
      cur.execute(sql_query)
      result = cur.fetchall()
      print(result)
      conn.commit()

标签: pythonsqlpython-3.xamazon-redshift

解决方案


我使用了以下代码:

query = "select distinct lower(tablename) from medaff.imedical_metadata where object_type = 'View'"
cur.execute(query)
res = cur.fetchall()
print(res)
res = tuple([item[0] for item in res])
res = str(res)

推荐阅读