3

我怎样才能简化这个更新指令?每当客户购买商品时,它都会将销售额更新为 1。

$this->db->query('SELECT amount FROM shop_items WHERE itemid='.$itemid.'');   
$new_amount = $item->amount+1;
    if(!$this->db->query('UPDATE shop_items SET amount='.$new_amount.' WHERE itemid='.$itemid.'')):
    return false;
    endif;

我不能让它更简单,所以行更少,例如:

if(!$this->db->query('UPDATE shop_items SET amount=_current_value_+1 WHERE itemid='.$itemid.'')):
return false;
endif;

这可能吗?如果还是不感谢

4

1 回答 1

6

使用以下 SQL 查询怎么样:

update shop_items
set amount = amount + 1
where itemid = 123

基本上,在一个 SQL 查询中,您将 设置amount为它之前的值加一。
不需要两个查询;-)


将它集成到您​​的 PHP 代码中应该会给您如下所示的内容:

if (!$this->db->query('UPDATE shop_items SET amount=amount+1 WHERE itemid='.$itemid.'')) :
    return false;
endif;

注意:我刚刚将您替换_current_value_为您正在处理的字段的名称:amount

于 2010-04-24T13:45:21.913 回答