mysql中的递减值但不是负数

问题描述 投票:0回答:7

当用户在 php 和 mysql 中删除它时,我想减少一个值。我想检查不要低于 0。如果值为 0,则不要 decrement.

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

如果字段是 0 那么什么都不做

mysql decrement
7个回答
89
投票

再添加一个条件,只有当

field
更大时才更新
0

UPDATE your_table 
SET field = field - 1
WHERE id = $number
AND field > 0

46
投票

您可以使用

GREATEST()
防止新值低于零。如果该值低于零,零将始终大于您的计算值,从而防止使用任何低于零的值。

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

附带说明:请不要再使用

mysql_*
功能。它们已被弃用,最终将从 PHP 中删除。使用 PDOMySQLi 代替。


12
投票

使用 GREATEST 的选项在较新的 MySQL 版本中不起作用,如果您想更新多个字段而不是一个字段,则接受的答案可能没有用。我对这个问题的解决方案是使用 IF:

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

3
投票
UPDATE table SET field = case when (field - 1) >0 then (field - 1)
else field end
WHERE id = $number

1
投票
 UPDATE `table_name` SET field = field-1 WHERE `id` = '".$id."' AND field > 0

注意:字段的数据类型应该是整数。


1
投票

如果字段是 int unsigned,下面是最好的:

UPDATE table 
SET field = field - 1
WHERE id = $number
and field > 0

# or
UPDATE  table
SET     field = IF(field > 0, field - 1, 0)
WHERE   id = $number

0
投票

对于使用 PDO 搜索示例的人 快...

<?php

/**
 * Class Set_quantity_product
 */
 class Set_quantity_product {
    
    
  /**
    * Set_quantity_product::update_quant_prod( $id, $number, $to_do );
    *
    * @param  {int} $id        a product id
    * @param  {int} $number    a number to increment or decrement for a value in DB
    * @param  {str} $to_do     'incr/decr'
    * @return {void}           increment or decrement but always return 0 if quant < 0
    */
    public static function update_quant_prod( $id, $number, $to_do ){
    
       $DATA_BASE = new PDO('mysql:host='.$YOUR_HOST.';dbname='.$YOUR_DB_NAME.';charset=utf8', $YOUR_USER_NAME, $YOUR_DB_PASSWORD, array( PDO::ATTR_PERSISTENT => false));
    
       // Note:
       // UPDATE  products
       // SET     quant = GREATEST( 0, quant+:ope )
       // WHERE   id = :id
       // increm or decrement but do nothing if quant is > quant in DB
    
       // pass quant number to affect to negative for decrement
       $number = ( $to_do == 'decr' ) ? $number*-1 : $number;
    
       // UPDATE ONE PROD. QUANT. 'ope' -> calcul operation
       $ARR_pdo = array( 'id' => (int) $id,
                            'ope' => $number );
       $sql = 'UPDATE products SET
          quant = IF(quant+:ope >= 0, quant+:ope, 0) WHERE id=:id';
    
       // prepa. SQL
       $request = $DATA_BASE->prepare($sql);
    
       // exec. request
       $request->execute($ARR_pdo);
    
       // PDO closeCursor
       $request->closeCursor();
    
       // return true for test
       return true;
   }
   /**
    * Set_quantity_product::update_quant_prod( $id, $number, $to_do );
    */
    
 }
 /**
  * Class Set_quantity_product
  */

?>

使用:(假设你有一个 id=42 的产品)

Set_quantity_product::update_quant_prod( 42, 5, 'decr' );

如果您在 DB 中的数量是 6 -> 这会将值设置为 1

如果您在 DB 中的数量是 5 -> 这会将值设置为 0

如果您在 DB 中的数量是 4 -> 这会将值设置为 0

© www.soinside.com 2019 - 2024. All rights reserved.