从codeigniter中的多个表中删除数据

时间:2017-11-27 05:36:11

标签: php mysql codeigniter security

我在codeigniter中使用删除查询从多个表中删除相同的ID数据

我的查询

public function delete_customer($id)
{
    $this->db->where('cus_id',$id);
    $this->db->delete('tbl_customer'); 
    $this->db->where('customer_id',$id);
    $this->db->delete('tbl_additional_details');
    $this->db->where('customer_id',$id);
    $this->db->delete('tbl_amc_supervisor');
    $this->db->where('cr_customer_id',$id);
    $this->db->delete('tbl_current_issues');
    $this->db->where('customer_id',$id);
    $this->db->delete('tbl_pickmachine');
    $this->db->where('customer_id',$id);
    $this->db->delete('tbl_sales');
    $this->db->where('customer_id',$id);
    $this->db->delete('tbl_service');
    return true;
}

任何简化和查询此查询的方法..

1 个答案:

答案 0 :(得分:0)

另一种方法是使用连接查询从多个表中删除类似

的表
DELETE 
  c,
  d,
  s,
  i,
  m,
  sa,
  se 
FROM
  tbl_customer c 
  LEFT JOIN tbl_additional_details d 
    ON (c.cus_id = d.customer_id) 
  LEFT JOIN tbl_amc_supervisor s 
    ON (c.cus_id = s.customer_id) 
  LEFT JOIN tbl_current_issues i 
    ON (c.cus_id = i.cr_customer_id) 
  LEFT JOIN tbl_pickmachine m 
    ON (c.cus_id = m.customer_id) 
  LEFT JOIN tbl_sales sa 
    ON (c.cus_id = sa.customer_id) 
  LEFT JOIN tbl_service se 
    ON (c.cus_id = se.customer_id) 
WHERE c.cus_id = @id 
  

注意在执行类似查询之前请先备份数据