2010-11-12 56 views
0

我在我的数据库“forum_traad”和“forum_kommentare”中有2个tabels,但他们有同一行“indhold”,所以当我尝试加入forum_traad和forum_kommentare时,我想回复“forum_traad “它回应”forum_kommentare“中的”隐藏“,我该怎么办?codeigniter加入问题

我的观点:

<div id="forum"> 


    <?php 
    if($query) 
    { 
    ?> 
    <div class="forum_headline">Forum kategori - Forum tråde - <?php echo $query->overskrift; ?></div><!-- forum_headline --> 
    <div class="forum_profil_img"></div><!-- forum_profil_img --> 
    <div class="forum_post_content"> 
    <span style="font-size:15px;"><?php echo anchor('profil/'.$query->brugernavn, $query->brugernavn); ?></span> 
    <span style="font-size:11px; margin-left:3px; color:#686868;"><i> Siger</i></span><br> 
    <?php echo $query->indhold; 
    echo "<br>ID: ".$query->id; 
    ?> 
    </div><!-- forum_post_content --> 


    <?php 
    } else { 
    echo "Der blev ikke fundet nogen post"; 
    } 
    ?> 

</div><!-- forum --> 

我的模型

function posts($id) 
{ 
    $this->db->select('*'); 
    $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; 
    } 

} 

回答

1

你可以给他们一个不同的名称:

$this->db->select('forum_traad.indhold as traad_indhold, 
        forum_kommentare.indhold as kommentare_indhold'); 

如果你需要的功能*,你除了可以选择:

$this->db->select('forum_traad.indhold as traad_indhold, 
        forum_kommentare.indhold as kommentare_indhold, 
        forum_traad.*, 
        forum_kommentare.*'); 
+0

当我尝试最后我得到这个错误 错误号码:1064 您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以便在'* FROM('forum_traad')附近使用正确的语法JOIN'forum_kommentare' ON'forum_kommentare'.'fk_forum_t'at line 1 SELECT'forum_traad'.'holdhold' as traad_indhold,'forum_kommentare'.'''''''''''kommentare_indhold,* FROM('forum_traad')JOIN'forum_kommentare' ON'forum_kommentare'.'fk_forum_traad' WHERE'forum_traad'.'id' ='13' – Simon 2010-11-12 22:49:15

+0

噢,对不起。 。当你将它与单个字段混合并且你有多个表时,你不能使用plain *。更正了这个例子。 – AndreKR 2010-11-12 22:53:01