我如何 select 并使用 codeIgniter 在单个查询中更新列值

How can I select and update a column value in single query with codeIgniter

我的数据库包含一个table,在这个table我有一个列"Earned"。此列包含一些值,我必须向此列添加一些金额(已赚取)。

现在我可以这样做..

        $this->db->select('Earned');
        $this->db->where('CustMobile', $LinkMobile);
        $query_result=$this->db->get('customers');
        $result=$query_result->row();

        $finalAmount=$result->Earned+$Commsion;

        $this->db->set('Earned', $finalAmount);
        $this->db->where('CustMobile', $LinkMobile);
        $this->db->update('customers');

我想知道如何使用单个查询更新此值?

我的数据库是这样的。

我想把这个圈出来的值1100改成2000

试试这个,

$this->db->query("UPDATE `customers` SET `Earned` = `Earned` + $Commsion WHERE `CustMobile` = $LinkMobile");

解释

Earned = Earned + $Commsion ==> 这部分将抓取字段的值并添加新值。

Example - If $Commsion is 1000 and your respective Earned field is 1100. So it will update as 2100.

虽然您已经接受了答案,但我想为其添加活动记录查询:

$this->db->set('Earned', 'Earned +'. $Commsion .'',false);
$this->db->where('CustMobile', $LinkMobile);
$this->db->update('customers');