gpt4 book ai didi

python - 如何使用 python-2.7 形成此 mysql 选择查询

转载 作者:行者123 更新时间:2023-11-29 07:24:36 25 4
gpt4 key购买 nike

我正在使用 Python-2.7 和 mysql 数据库,希望使用 select 语句通过传递 2 个或 2 个参数中的任意一个来从表 Student 检索数据,如下所示 -

从student_details中选择*,其中student_city='some_City'或student_stream='Science'或两者

参数将传递给操作 SELECT 语句的函数。

任何形式的指导/帮助,让我了解如何编码 where 子句部分。我不想进行单独的查询。

如果我错误地提出问题或重复问题,请道歉。预先感谢:)

最佳答案

我尝试过,最后这对我有用 -

 try:
query = ("""SELECT * FROM student_details WHERE """)
if city is not '0' and Stream == '0':
complete_query = query+("""student_city = %s""")

elif Stream is not '0' and city == '0':
complete_query = query+("""student_stream = %s""")

else:
complete_query = query+("""student_city = %s AND student_stream = %s""")

if city is not '0' and Stream == '0':
s_execute = self.cur2.execute(complete_query,(city,))

elif Stream is not '0' and city == '0':
s_execute = self.cur2.execute(complete_query,(Stream,))

else:
s_execute = self.cur2.execute(complete_query,(city),(Stream))
except MySQLdb.Error as error:
print(error)

这与上面的建议有些相似。谢谢大家指导。

关于python - 如何使用 python-2.7 形成此 mysql 选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34941475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com