Kevinster
Kevinster

Reputation: 67

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?

Upvotes: 0

Views: 116

Answers (3)

fthiella
fthiella

Reputation: 49089

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

Upvotes: 2

Elon Than
Elon Than

Reputation: 9775

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

Upvotes: 2

juergen d
juergen d

Reputation: 204894

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

Upvotes: 0

Related Questions