python - 使用一个查询的输出输入另一个查询 python

标签 python mysql python-2.7

以下是我的代码,

connect=Tl.connection.Connection()
a= connnect.connect('dbname', schema='schemaname')
q='''select id, id1 from table1;'''
w= Tl.datatool.todf(a(q))
id=w.id
id1=w.id1

现在对于每个 id 和 id1,我需要执行第二个查询,它应该像一个循环并且应该存储在数据帧中,

我正在寻找的查询是,

select id2 from table2 where x=id and y=id1;

我正在努力

for i in id:
    for j in id1:
        q2='''select id2 from table2 where x=%i and y=%i;''' (int(id), int(id1))
        print a(q2)

但我无法获得确切的输出。我收到以下错误,

   TypeError: 'str' object is not callable

我正在寻找的输出是,对于 id 和 id1 的所有值,我需要获取 id2 并且所有值都应存储在数据框中。

有人可以帮我做这件事吗?

最佳答案

您忘记使用%运算符:

    q2='''select id2 from table2 where x=%i and y=%i;''' (int(id), int(id1))

应该阅读

    q2='''select id2 from table2 where x=%i and y=%i;''' %(int(id), int(id1))

关于python - 使用一个查询的输出输入另一个查询 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31840609/

相关文章:

php - 为什么此查询返回 2013 年的项目?

python - 这是自动化无聊的东西的一段代码

python - 通过 Python Requests 库发送文件与 Curl 有何不同?

python - 使用 Tesseract OCR 从表格图像中识别特定数字

python - 测试两个对象是否相同而不仅仅是相等的Pythonic方法是什么

mysql - 将 DateTime 属性映射到时间戳 mysql+fluent nhibernate

mysql - 当我在 MySQL 中使用 'select' 时,如何将表中的所有日期放入新列中?

python - 使用正则表达式删除特殊的 Unicode 字符?

python - python 动态条形图生成错误

python - 仅当 i<j 时将列表元素 list[i] 和 list[j] 相乘的代码