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

时间:2015-08-05 18:54:26

标签: 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并且所有值都应该存储在数据帧中。

有人可以帮我这么做吗?

2 个答案:

答案 0 :(得分:0)

这应该有用,但它与SQL非常不同。实际上,通过执行此操作,您将绕过关系数据库的功能。您应该使用子查询;使用官方MySQL文档获取更多信息。

答案 1 :(得分:0)

您忘记使用%运算符:

    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))
相关问题