如何简化此数据库更新查询(php)

发布于 2024-08-30 11:06:09 字数 529 浏览 2 评论 0原文

我怎样才能简化这个更新指令?每当客户购买商品时,它都会将销售额更新为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2024-09-06 11:06:09

使用以下 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

What about using the following SQL query :

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

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 :

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

Note : I just replaced your _current_value_ by the name of the field that you're working on : amount.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文