2010-11-13 43 views
0

你好我有這樣的加盟,但我想加入一個名爲「用戶」另一TABEL和我想要的「profil_billed」行,我怎麼能做到這一點笨多加入

我的意思是「WHERE forum_traad.brugernavn =用戶。 profil_billed」類似的東西

function posts($id) 
{ 
    $this->db->select('*,forum_traad.indhold as traad_indhold, 
         forum_kommentare.indhold as kommentare_indhold, 
         forum_traad.brugernavn as traad_brugernavn, 
         forum_traad.id as traad_id 
         '); 
    $this->db->from('forum_traad'); 
    $this->db->join('forum_kommentare', 'forum_kommentare.fk_forum_traad', 'forum_traad.id'); 
    $this->db->where('forum_traad.id', $id); 

    $query = $this->db->get(); 

    if($query->num_rows > 0) 
    { 
     return $query->row(); 
    } else { 
     return false; 
    } 

} 
+0

類似的東西 $這個 - > DB->加入( '用戶', 'forum_traad.brugernavn', 'users.username'); – Simon 2010-11-13 18:07:03

+0

但它不工作,我可以有2個連接像這樣? – Simon 2010-11-13 18:07:23

回答

0

你可以寫你的查詢,如下 -

$this->db->select('*,forum_traad.indhold as traad_indhold, 
         forum_kommentare.indhold as kommentare_indhold, 
         forum_traad.brugernavn as traad_brugernavn, 
         forum_traad.id as traad_id, users.profil_billed as billed 
         '); 
    $this->db->from('forum_traad'); 
    $this->db->join('forum_kommentare', 'forum_kommentare.fk_forum_traad = forum_traad.id'); 
    $this->db->join('users', 'forum_traad.brugernavn = users.profil_billed'); 
    $this->db->where('forum_traad.id', $id); 
    $query = $this->db->get(); 

因爲我已經使用的語法的詳細文檔請參考下面 -

http://codeigniter.com/user_guide/database/active_record.html

+0

它不工作就轉到我的其他在我看來,文件看起來像這樣: http://pastebin.com/45muaLLY – Simon 2010-11-13 19:09:24

+0

你得到一個MySQL錯誤或零行回來了?你是否需要非自然連接,如左連接? – Natebot 2010-11-13 23:40:33

+0

我只得到這個「Der blev ikke fundet nogentråd」,那是我的其他人,所以不..我不知道我是否需要左連接 – Simon 2010-11-13 23:47:12