简体   繁体   中英

Easier way to update MySQL row based on previous data?

I was wondering if there was an easier way to go about updating data in my MySQL database. I have a column called "food" which is an integer.

Whenever I want to update the amount of food, I have to SELECT the current amount, create a new variable for $new_food which is the current food count + amount of food to add. Then I have to run the UPDATE query to shove $new_food in.

This seems pretty inefficient. Is there an easier way to go about updating my food column?

UPDATE查询可以访问该列的先前值。

UPDATE sometable SET food=food+:morefood WHERE id=:someid

您可以直接在UPDATE查询中增加列:

UPDATE tbl_name SET col_name = col_name + 123 WHERE ...;

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