Decrementar valor en mysql pero no negativo


Quiero disminuir un valor cuando el usuario lo elimine en php y mysql. Quiero comprobar no ir por debajo de 0. Si el valor es 0 entonces no decrementar .

mysql_query("UPDATE table SET field = field - 1 WHERE id = $number");

Si el campo es 0 entonces no hagas nada

Author: Shikiryu, 2013-04-29

6 answers

Agregue otra condición para actualizar solo si el field es mayor 0

UPDATE table 
SET field = field - 1
WHERE id = $number
and field > 0
 58
Author: juergen d,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-11 01:46:08

Puede evitar que el nuevo valor caiga por debajo de cero utilizando GREATEST(). Si el valor cae por debajo de cero, cero siempre será mayor que su valor calculado, evitando así que se use cualquier valor por debajo de cero.

UPDATE  table
SET     field = GREATEST(0, field - 1)
WHERE   id = $number

Y en una nota al margen: Por favor, no utilice más funciones mysql_*. Están obsoletos y eventualmente serán eliminados de PHP. Utilice PDO o MySQLi en su lugar.

 30
Author: Till Helge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-29 11:22:22

La opción usando GREATEST no funcionará en las versiones más recientes de MySQL, y la respuesta aceptada puede ser inútil si desea actualizar varios campos en lugar de uno. Mi solución para este problema es usar IF:

UPDATE  table
SET     field = IF(field > 0, field - 1, 0)
WHERE   id = $number
 6
Author: Eelco Luurtsema,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-07 15:53:22
UPDATE table SET field = case when (field - 1) >0 then (field - 1)
else field end
WHERE id = $number
 1
Author: Niket,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-28 18:13:24

Puede cambiar su campo para ser UNSIGNED, y añade IGNORE a tu consulta:

UPDATE IGNORE table SET field = field - 1 WHERE id = $number

ACTUALIZACIÓN-Esto no funciona

 -1
Author: Narek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:26:36
 UPDATE `table_name` SET field = field-1 WHERE `id` = '".$id."' AND field > 0

Nota: el tipo de datos del campo debe ser un ENTERO.

 -1
Author: Aatif,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-09 12:31:11