如何简化此数据库更新查询(php)
我怎样才能简化这个更新指令?每当客户购买商品时,它都会将销售额更新为 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;
这可能吗?如果没有的话还是谢谢
How could I simplify this update instruction? Whenever a client buys an item it will update the sales with 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;
Can't I make it more simple so there are less lines, for example:
if(!$this->db->query('UPDATE shop_items SET amount=_current_value_+1 WHERE itemid='.$itemid.'')):
return false;
endif;
is this possible? if not thanks anyway
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用以下 SQL 查询怎么样:
基本上,在一个 SQL 查询中,您将
amount
设置为它之前的值加一。不需要两次查询;-)
将其集成到您的 PHP 代码中应该会为您提供如下所示的内容:
注意:我刚刚将您的
_current_value_
替换为您正在处理的字段的名称:amount
。What about using the following SQL query :
Basically, in just one SQL query, you set the
amount
to the previous value it had, plus one.No need for two queries ;-)
Integrating this in your PHP code should give you something that looks like this :
Note : I just replaced your
_current_value_
by the name of the field that you're working on :amount
.