如何在主键和外键的基础上联接两个表进行搜索


How to join two table for search on base of primary and foreign key?

我有两个表,一个是表1

id名称价格

1名称1 100

2名称2 200

表2

id机构id机构开始日期结束日期

1 1机构1 1-2-2015 2-12015

我想在第二个表中搜索对我来说很好的值但是我想要基于外键(table1_id(的第一个表中的名称。我该怎么做。有什么需要帮忙的吗?

这是我的代码

控制器:

public function searchResult()
{
    $search_term = array(
    'authorityId' => $this->input->get('authority'),
    'grantVillage' => $this->input->get('village'),
    'startDate' => $this->input->get('startDate'),
    'endDate' => $this->input->get('endDate'));
    //print_r($search_term);
    $data['searchResult'] = $this->grant_model->searchResult($search_term);
    $this->load->view('searchResult',$data);
}

型号:

 public function searchResult($search_term)
    {
           $this->db->select('*');
           $this->db->from('grant_data');
           $this->db->like('authorityId', $search_term['authorityId']);
           $this->db->like('grantVillage', $search_term['grantVillage']);
           $this->db->like('startDate', $search_term['startDate']);
           $this->db->like('certificate', $search_term['certificate']);
           $this->db->like('endDate', $search_term['endDate']);
           $query = $this->db->get();
           return $query->result();
    }

请这样使用--

   $this -> db -> select('*');
   $this -> db -> from('table2');
   $this->db->join('table1','table1.table1_id = table2.autority_id');
   $this -> db -> where('table2'.'id', 5);
   $query = $this -> db -> get();
   return $query->result_array();

它将获得两个表数据,请尝试…

public function searchResult($search_term)
    {
           $this->db->select('*');
           $this->db->from('grant_data');
           $this->db->like('authorityId', $search_term['authorityId']);
           $this->db->like('grantVillage', $search_term['grantVillage']);
           $this->db->like('startDate', $search_term['startDate']);
           $this->db->like('certificate', $search_term['certificate']);
           $this->db->like('endDate', $search_term['endDate']);
           $query = $this->db->get();
           return $query->result();
    }

运行后,此查询结果为

id foreign_key字段1字段2字段3

现在我想从另一个表中以外键为基础的名称

我该怎么做?