2017-05-31 10 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(); 
}