php - Codeigniter JOIN 多个表

标签 php mysql codeigniter

我在使用 codeigniter 检索多个表中的数据时遇到了一些麻烦。

这是我用来在我的模型中检索数据的代码,运行良好。

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;
}

现在我尝试使用简化代码但无法显示数据。这是我模型中的代码

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();
}

在我的 Controller 中,我使用这段代码在我的模型中检索数据

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);
}

对于显示我使用了这段代码

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'];
}

我根本无法显示任何东西。请帮忙

最佳答案

试试下面的代码,

我相信你会想要这样的东西:

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();
}

关于php - Codeigniter JOIN 多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052852/

相关文章:

php - codeigniter 的负载如何工作?

php - Guzzle ~6.0 多部分和 form_params

PHP,简单锁定所有请求

php - 使用 mySQL 进行下拉搜索查询

mysql - table 掉下来后 hive 坏了

java - java和mysql中相同字符串的不同unicode

php - 将动态生成的表单数据插入mysql

php - Codeigniter 日历不显示正确的数据并且设置限制不起作用

javascript - 如何正确地将 retina.js 排入 WordPress 子主题functions.php 中?

php - 插入查询在 codeigniter 中不起作用