简体   繁体   English

对于3个表,where子句中的“ id”列含糊不清

[英]Column 'id' in where clause is ambiguous for 3 tables Codeigniter

I am currently doing a system. 我目前正在做一个系统。 In the system, there's a module called manage resident; 在系统中,有一个模块称为“常驻管理”。 where the manage resident will get the information of the resident if it is single, married and have a child(first,middle and last name for example). 如果单身,已婚并有孩子(例如,名字,中间名和姓氏),管理居民将获得居民的信息。

I've done the adding or create new resident part, I am now in edit resident part. 我已经完成了添加或创建新的常驻部分,现在我正在编辑常驻部分。 When I click the edit resident, I encountered error 当我单击编辑常驻程序时,遇到错误

Column 'id' in where clause is ambiguous where子句中的“ id”列不明确

I have 3 tables: resident, married and children. 我有3张桌子:居民,已婚和孩子。 Resident ID is the foreign key of married, while Married ID is the foreign key of children table. 居民ID是已婚的外键,而已婚ID是子表的外键。

My data example for each table(column Primary key, foreign key, first_name, middle_name, last_name 每个表的数据示例(列主键,外键,first_name,middle_name,last_name

Resident -> id, Testing, Testing, Testing
Married  -> Married_id, resident_id, Testing, Testing, Testing
Children -> id, married_id, Testing, Testing, Testing

Question: How can I get the children of the selected Resident? 问题:如何获得所选居民的孩子? Then display it separately in my fields in my view. 然后在我的视图中的字段中分别显示它。

Controller 调节器

 $id = $this->uri->segment(4);
 $get_children = $this->Crud_model->get_children($id);

Model 模型

public function get_children($id){
        $this->db->select('*');    
        $this->db->from('resident');
        $this->db->where('id',$id);
        $this->db->join('married', 'resident.id = married.resident_id');
        $this->db->join('children', 'resident.id = children.married_id');
        $query = $this->db->get();
        return $query->row();
    }

View 视图

 <?php $explode = explode(",",$children->first_name); ?>

<div class="form-group">
  <label>First Name</label>
  <input type="text" class="form-control" name="child_fname" id="child_fname"><?= $explode?>
</div>

Whenever you join tables with where condition you should write each column with its reference table name. 每当您在具有where条件的情况下联接表时,都应使用其引用表名称编写每列。

so change 所以改变

$this->db->where('id',$id);

to

$this->db->where('resident.id',$id);

You can try this solution for your problem: 您可以尝试解决此问题的方法:

Query: 查询:

public function get_children($id){
   $this->db->select('*');    
   $this->db->from('resident');
   $this->db->where('resident.id',$id);
   $this->db->join('married', 'resident.id = married.resident_id', 'left');
   $this->db->join('children', 'resident.id = children.married_id','left');
   $query = $this->db->get();
   return $query->row();
}

I Hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM