如何使用内部联接更新Codeigniter中的表?

时间:2019-04-11 08:18:03

标签: php jquery mysql codeigniter

我想在查询生成器Codeigniter中转换此查询Mysql

该查询在mysql中有效,但在codeigniter中无效

Mysql

UPDATE tbl_person p
INNER JOIN tbl_family_victim f  ON  p.id_person = f.id_person
SET p.active = 0
WHERE f.id_prisoners = 32;

Codeigniter

public function delete_all($id_pris){
   $this->db->join('tbl_family f','p.id_person = f.id_person');
   $this->db->set('p.active', '0');
   $this->db->where('f.id_prisoners', $id_pris);
   $data_result = $this->db->update('tbl_person p');
   return  $data_result;
}

2 个答案:

答案 0 :(得分:1)

您的脚本中有分钟错误,您错过了从属表的别名

版本1

public function delete_all($id_pris){
   $this->db->join('tbl_family f','p.id_person = f.id_person',"inner"); // you missed `f` here
   $this->db->set('p.active', '0');
   $this->db->where('f.id_prisoners', $id_pris);
   $data_result = $this->db->update('tbl_person p');
   return  $data_result;
}

这应该很好。

版本2

$this->db->set('p.active', '0');
$this->db->where('f.id_prisoners', $id_pris);
$this->db->where('p.id_person = f.id_person')
$data_result = $this->db->update('tbl_family as f, tbl_person as p');
return $data_result;

版本3

$sql = "
UPDATE tbl_person p
INNER JOIN tbl_family_victim f  ON  p.id_person = f.id_person
SET p.active = 0
WHERE f.id_prisoners = $id_pris";
$this->db->query($sql);

版本4

$this->db->set('p.active', '0');
$this->db->where('f.id_prisoners', $id_pris);
$data_result = $this->db->update('tbl_family as f JOIN  tbl_person as p ON p.id_person = f.id_person');
return $data_result;

答案 1 :(得分:0)

尝试一下,

public function delete_all($id_pris){
   $this->db->set('p.active', '0');
   $this->db->where('f.id_prisoners', $id_pris);
   $data_result = $this->db->update('tbl_person p JOIN tbl_family f ON p.id_person = f.id_person');
   return  $data_result;
}