【发布时间】:2021-08-23 02:47:27
【问题描述】:
我已经在这里搜索了几个解决方案并试图获得一个有效的代码。除了 where 查询外,一切正常。
在 where 查询中,我搜索最大值(数字)。然而,这并没有真正的工作...... 这是我的代码和MySQL数据库的结构。
谢谢!
import pymysql
conn = pymysql.connect(host='localhost', unix_socket='', user='root', passwd='pw', db='database')
cur = conn.cursor()
cur.execute("SELECT * FROM dose")
for r in cur:
curr = conn.cursor()
sql = """UPDATE dose
SET status = "printed"
WHERE id = SELECT GREATEST (status) FROM dose (status);"""
# print(sql)
try:
# Execute the SQL command
curr.execute(sql)
# Commit your changes in the database
conn.commit()
except:
# Rollback in case there is any error
conn.rollback()
curr.close()
cur.close()
conn.close()
【问题讨论】:
-
你的问题不清楚。请在此处添加示例数据。
-
状态栏是数字还是字符串?
-
我建议你在你的 MySQL 终端中运行查询,看看它是否在那里工作。如果 status 是一个数字,那么 SET status =“printed” 似乎不太可能起作用。此外,您的 sql 语句周围似乎有太多引号。
-
SELECT GREATEST (status) FROM dose (status);的输出你喜欢吗? -
(@DS_London
too many quotesPython 三引号:允许字符串文字包含换行符。推荐用于doc strings,甚至单行。)
标签: python mysql groupwise-maximum