两个SELECT语句NESTED

时间:2017-10-06 11:03:03

标签: python tornado psql

我一直在尝试执行以下查询,但它没有给我任何输出。

self.db.execute("SELECT * FROM patients WHERE patients.doctorid = (SELECT id FROM doctors WHERE username = '%s' % (usr)), callback=self.add_response)

我试图通过硬编码来执行相同的查询,它给了我正确的输出。

select * from patients where patients.doctorid = (select id from doctors where username = 'admin');

有人能说出错误吗?

1 个答案:

答案 0 :(得分:0)

尝试像这样执行:

 self.db.execute("SELECT * FROM patients WHERE patients.doctorid = (SELECT id FROM doctors WHERE username = '%s')" % (usr), callback=self.add_response)

我用"关闭了SQL查询并修正了括号。

您也可以按照here所示尝试:

self.db.execute("SELECT * FROM patients WHERE patients.doctorid = (SELECT id FROM doctors WHERE username = '%s';", (usr), callback=self.add_response)