2015-11-04 90 views
-1

我想在CodeIgniter中一起加入兩個查詢,第二個查詢將依賴來自第一個查詢的值。我希望得到的數組包含公告表中的所有數據,PLUS根據第一個查詢中的poster_id從用戶表中檢索到的第一個和最後一個名稱。加入兩個MySQL查詢並基於第一個查詢中的值檢索

例如,在僞代碼:

SEECT * FROM announcements JOIN SELECT first, last FROM users WHERE user_id = announcements.poster_id

這裏是我的代碼:

$this->db->order_by('time_posted', 'DESC'); 
$query = $this->db->get('announcements'); 
$results = $query->result(); 

這是可能的,我怎麼會修改這包括上述連接查詢?

+0

來吧。從基礎開始。我們都必須。 – Strawberry

回答

1

你可以嘗試,做一個適當的加入:

$this->db->select('a.*, u.first, u.last'); 
$this->db->from('announcements a'); 
$this->db->join('users u', 'u.user_id = a.poster_id'); 
$this->db->order_by('time_posted', 'DESC'); 
$query = $this->db->get(); 
0

你也可以試試這個。

$this->db->select('announcements.*, users.first, users.last'); 
$this->db->from('announcements'); 
$this->db->join('users', 'users.user_id = announcements.poster_id'); 
$this->db->order_by('time_posted', 'DESC'); 
$query = $this->db->get(); 
$results = $query->result();