嗨,这个查询我做错了什么?

时间:2017-10-24 11:04:04

标签: php codeigniter

我想从收入表中选择收入金额,更新总收入并从总收入中减去收入。然后删除该行。

public function delete($id)
{ 
    $this->db->get('income');
    $this->db->select('income_amount');
    $this->db->query('update total_income set amount=amount-'.$this->db->select('income_amount'));

    $this->db->where('income_id', $id);
    $this->db->delete('income');    
}

1 个答案:

答案 0 :(得分:1)

除非我误解了您的问题,否则您需要执行以下操作:

  1. income_amount表中选择income

    $this->db->select('income_amount');
    $query = $this->db->get('income');
    $row = $query->row();
    
    if (isset($row)) {
        $income_amount = $row->income_amount;
    }
    
  2. 通过从所有表格的total_income值中删除$income_amount来更新amount表格:

    $this->db->set('amount', 'amount-'.$income_amount);
    $this->db->update('total_income');
    
  3. $id表中删除income标识的行:

    $this->db->delete('income', array('income_id' => $id));