从Codeigniter中的两个以上表中获取数据

时间:2017-12-10 19:52:52

标签: php database codeigniter join

这是我的模型代码

$this->db->select('*');
$this->db->from('vf_training_district', 'vf_training_firm', 'complain_form');
$this->db->where('complain_form.InstituteId', 'vf_training_firm.FirmId');
$this->db->where('complain_form.DistrictId', 'vf_training_district.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();

获取未知列的错误抱怨_form.InstituteId。每个列都与db

中的相同

2 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

 $this->db->select(*);
 $this->db->from('complain_form cf');
 $this->db->join('vf_training_firm vftf', 'vftf.FirmId = cf.InstituteId');
 $this->db->join('vf_training_district vftd', 'vftd.DistrictId = cf.DistrictId');

 $query = $this->db->get();
 return $result = $query->result_array();

了解更多信息: https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

答案 1 :(得分:0)

您需要JOIN

SELECT *
  FROM complain_form 
  JOIN vf_training_firm ON complain_form.InstituteId = vf_training_firm.FirmId 
  JOIN vf_training_district ON complain_form.InstituteId = vf_training_district.DistrictId

使用Query Builder类Codeigniter:

$row = $this->db->select(*)
                ->from('complain_form')
                ->join('vf_training_firm', 'complain_form.InstituteId = vf_training_firm.FirmId')
                ->join('vf_training_district', complain_form.InstituteId = vf_training_district.DistrictId)
                ->get();

return $row->result_array();