2017-05-31 139 views
0

我有3个表guest_user_info,pg_company,user_profile。加入3个表codeigniter php

在guest_user_info有2列:

g_uid | company_id 

在pg_company有2列:

company_id | user_id 

在user_profile有2列:

id |user_email 

在这里,我想user_emailuser_profile.ig_uid值(在guest_user_info表)。我想从guest_user_info COMPANY_ID并获得COMPANY_ID和配合pg_company表中,有我可以user_id.then匹配与user_id与ID在user_profile我需要table.at最后user_emailuser_profile

回答

0

那么其一个简单的,你只需要在CodeIgniter中使用活动查询加入。

$this->db->select("UP.id", "UP.user_email"); 
$this->db->from("guest_user_info AS GU"); 
$this->db->join("pg_company AS PC", "PC.company_id=GU.company_id"); 
$this->db->join("user_profile AS UP", "UP.id=PC.user_id"); 
$this->db->where("GU.g_uid", $guid); 
$query = $this->db->get(); 

return $query->result(); 

在上面的代码,$guid你必须提供你有。

也请看看这些链接:

https://www.codeigniter.com/userguide3/database/query_builder.html

https://www.codeigniter.com/userguide2/database/active_record.html

你看完这个得到如此多的东西。

+0

谢谢。你工作正常 –

+0

upvote如果你有正确的答案。 – kishor10d

0
Check bellow code it`s working fine and common model function also 
    supported more then one join and also supported multiple where condition 
order by ,limit.it`s EASY TO USE and REMOVE CODE REDUNDANCY. 

    ================================================================ 
    *Album.php 
    //put bellow code in your controller 
    ================================================================= 

    $album_id='';//album id 

    //pass join table value in bellow format 
    $join_str[0]['table'] = 'pg_company'; 
    $join_str[0]['join_table_id'] = 'pg_company.company_id'; 
    $join_str[0]['from_table_id'] = 'guest_user_info.company_id'; 
    $join_str[0]['join_type'] = '';//set join type 

    $join_str[1]['table'] = 'user_profile'; 
    $join_str[1]['join_table_id'] = 'user_profile.id'; 
    $join_str[1]['from_table_id'] = 'guest_user_info.user_id'; 
    $join_str[1]['join_type'] = ''; 


    $selected ="guest_user_info.*,user_profile.user_name,pg_company.name"; 
    $condition_array=array('guest_user_info.g_uid' => $g_uid);  
    $albumData= $this->common->select_data_by_condition('guest_user_info', $condition _array, $selected, '', '', '', '', $join_str); 
           //call common model function 

    if (!empty($albumData)) { 
     print_r($albumData); // print album data 
    } 

========================================================================= 
    Common.php 
    //put bellow code in your common model file 
======================================================================== 

    function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) { 
    $this->db->select($data); 
    //if join_str array is not empty then implement the join query 
    if (!empty($join_str)) { 
     foreach ($join_str as $join) { 
      if ($join['join_type'] == '') { 
       $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']); 
      } else { 
       $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']); 
      } 
     } 
    } 

    //condition array pass to where condition 
    $this->db->where($condition_array); 


    //Setting Limit for Paging 
    if ($limit != '' && $offset == 0) { 
     $this->db->limit($limit); 
    } else if ($limit != '' && $offset != 0) { 
     $this->db->limit($limit, $offset); 
    } 
    //order by query 
    if ($sortby != '' && $orderby != '') { 
     $this->db->order_by($sortby, $orderby); 
    } 

    $query = $this->db->get($tablename); 
    //if limit is empty then returns total count 
    if ($limit == '') { 
     $query->num_rows(); 
    } 
    //if limit is not empty then return result array 
    return $query->result_array(); 
}