python - 如何在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()
解决方案
我使用了以下代码:
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)
推荐阅读
- php - Html 和 css 到电子邮件 php laravel
- javascript - 赛普拉斯 - 错误消息与代码不匹配
- c++ - CSES - 不同的颜色
- php - WooCommerce 针对特定产品类别的累进数量折扣
- css - CSS网格没有调整到屏幕的宽度
- wordpress - Flutter Wordpress:在 null 上调用了 getter 'length'。接收方:null 尝试调用:长度
- database - 将包含多个 jsonl 文件的多个文件夹加载到 GCP 中的云数据库中(例如:BigQuery)
- javascript - 谁能解释为什么这适用于类组件而不是功能组件?
- javascript - 单击时为每个单独的 TD 打开跨度
- excel - 如何使用 Access VBA 复制 Outlook 电子邮件正文并粘贴到 Excel 中?