mysql - 更新值是当前值加上新值

时间:2014-08-21 07:53:59

标签: php mysql pdo sql-update

我有以下更新查询。 shots_limit需要是数据库中的当前值PLUS我的新值$add_shots。使用绑定时有没有办法做到这一点?

//figure out how many screenshots to add
$add_shots = $_POST['Quantity'] * 500;

$stmt = $db->prepare("
    UPDATE accounts 
    SET orders = :orders,
        shots_limit = :shots_limit
    WHERE account_id = :account_id  
");

//bindings
$binding = array(
    'orders' => $orders_str,
    'shots_limit' => $add_shots + ORIGINAL VALUE
    'account_id' => $result['account_id']
);

$status = $stmt->execute($binding);

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西。只需在更新中使用当前值

$stmt = $db->prepare("
    UPDATE accounts 
    SET orders = :orders,
        shots_limit = shots_limit + :shots_limit
    WHERE account_id = :account_id  
");

//bindings
$binding = array(
    'orders' => $orders_str,
    'shots_limit' => $add_shots,
    'account_id' => $result['account_id']
);