简体   繁体   English

Codeigniter联接多个表

[英]Codeigniter JOIN multiple tables

I'm having a little trouble in retrieving data in multiple tables using codeigniter. 我在使用Codeigniter检索多个表中的数据时遇到了一些麻烦。

This is the code i'm using to retrieve data in my model which is working well. 这是我用来在模型中检索数据的代码,效果很好。

function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;  
}

function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}

Now i tried using a simplified code but fails to display the data. 现在,我尝试使用简化的代码,但无法显示数据。 here is the code in my model 这是我模型中的代码

function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}

In my controller, i used this code to retrieving data in my model 在控制器中,我使用此代码来检索模型中的数据

function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}

and for the display i used this code 对于显示,我使用了这段代码

foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name']; 
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}

I cant display anything at all. 我什么都不显示。 Please help 请帮忙

Hope below mentioned function should return data that you expected. 希望下面提到的函数应返回您期望的数据。

function retrieve_all_data($alumni_id)
{
  $this->db->select('*');
  $this->db->from('experience ex');
  $this->db->join('education ed','ed.alumni_id=ex.alumni_id');
  $this->db->where('ex.alumni_id',$alumni_id);
  $query=$this->db->get();
  return $query->result_array();
}

Try the below code, 试试下面的代码,

I believe you'll want something like this: 我相信您会想要这样的东西:

function retrieve_all_data($alumni_id)
{
  $this->db->select("e.*,edu.*");
  $this->db->from("experience e");
  $this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
  $this->db->where('e.alumni_id',$alumni_id);
  $this->db->group_by('e.exp_id');
  $query = $this->db->get();
  return $query->result_array();
}

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

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