2016-07-15 127 views
0

我正在使用代碼點火器實現連接查詢。我有兩個表Codeigniter根據兩個連接條件連接兩個表

1) Users -> Contains all users 
2) Query -> Contains queries assigned to users. Each query has two users to attend that query. 

在查詢表我有兩列

1) attendingMD -> First user attending that query 
2) secondAttendingMD -> Second user that query. 

我要顯示的查詢列表中有兩個用戶參加該查詢的姓名。我設法通過使用此代碼獲得第一個用戶的姓名。

$this->db->select("Query.*, Users.fullname as firstMD"); 
    $this->db->join('Users', 'Users.id = Query.attendingMD'); 
    $this->db->where('Query.isCompleted', 1); 
    $query = $this->db->get('Query'); 
    return $query->result_array(); 

請建議我該如何做到這一點。 這裏是我想要顯示錶格的視圖。 enter image description here

回答

5

嘗試:

$this->db->select("Query.*, uf.fullname as firstMD, us.fullname as secondMD"); 
$this->db->join('Users uf', 'uf.id = Query.attendingMD'); 
$this->db->join('Users us', 'us.id = Query.secondAttendingMD'); 
$this->db->where('Query.isCompleted', 1); 
$query = $this->db->get('Query'); 
return $query->result_array(); 
+0

非常感謝,它的工作就像一個魅力。你真了不起。 –