简体   繁体   中英

Set a max value for MYSQL field

I'm currently using the following code to update a certain field every hour

mysql_query("UPDATE  `admin_wp12`.`wp_usermeta` SET  meta_value=meta_value+3 WHERE `wp_usermeta`.`umeta_id` =17;");

However, I want it to stop increasing when the meta_value reaches 300.

I really don't get the use of the MAX function, any ideas?

Just update fields with meta_value equal or lower than 297 (because on 297 we will allow one more increase to 300).

UPDATE `admin_wp12`.`wp_usermeta`
SET meta_value=meta_value + 3
WHERE `wp_usermeta`.`umeta_id` = 17
   AND `wp_usermeta`.`meta_value` <= 297

If you want to allow a value like 299 to be updated as 300, you could use the LEAST function like this:

UPDATE `admin_wp12`.`wp_usermeta`
SET
  meta_value=LEAST(300, meta_value+3)
WHERE
 `wp_usermeta`.`umeta_id` = 17
 AND `wp_usermeta`.`meta_value` < 300

Add an additional condition to the end of your query

UPDATE admin_wp12.wp_usermeta 
SET  meta_value = meta_value + 3 
WHERE umeta_id = 17
AND meta_value <= 297

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