[英]How update a table in codeigniter with inner join?
我想在查詢生成器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;
點火槍
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;
}
嘗試這個,
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;
}
您的腳本中有分鍾錯誤,您錯過了從屬表的別名
版本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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.