简体   繁体   中英

Python/MySQL SELECT, Update and Fetch in same query

I want to SELECT and UPDATE in one query.

In my database, I have category_id s that needs to be checked. I want to select the last updated query by DESC and update it with runs = runs+1 , so it counts up that and also automatically update the last_update field.

But for some reason, I can't fetch it

cursor.execute(
"UPDATE categorys as t1, 
(SELECT category_id 
 FROM categorys 
 ORDER BY last_update 
 LIMIT 1) as t2 
 SET t1.runs = t1.runs+1 
 WHERE t2.category_id = t1.category_id")
row = cursor.fetchone()
print(row)

print(row) gives me None but I need the category_id here

The update works and counts up the runs+1 of the last_update DESC

mysql update doesn't return value of data changed. It only returns the Query ran ok.

you would need to execute the select statement to return the category_id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM