CodeIgniter加入问题

时间:2010-11-14 22:54:44

标签: php mysql codeigniter

我想从3个表中选择“Forum_traad”“Forum_kommentare”“用户”

我想收集“forum_traad”和“forum_kommentare”中的所有字段,只收集我的用户表中的字段“profil_billed”,我想显示适合创建该帖子的用户的图片,以及创建该帖子的用户在线程中的响应。

我在“forum_traad”和“forum_kommentare”中有一个名为“Brugernavn”的字段,它必须在“用户”表格中匹配“Brugernavn”。

希望你理解我的意思,这是我到目前为止所做的:

模型文件:

 $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();

我的观看文件:

 if($query) 
 {
 echo $query->overskrift;
 } else {
 echo "der er ikke noget";
 }

1 个答案:

答案 0 :(得分:1)

一条简单的建议:当您发布问题时尝试将数据(您的代码)更改为英语...并描述一些参数 - 数据在您的程序中意味着什么,数据库表中的关系,fk-s ...在这种情况下,更多的人会试图帮助你,而且他们的某些事情看起来会比他们必须破译这里发生的事情更加明显......

所以我会尽力帮助:

这是你的echo sql:

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 
FROM (forum_traad) 
    JOIN forum_kommentare ON forum_kommentare.fk_forum_traad = forum_traad.id 
    JOIN users ON forum_traad.brugernavn = users.profil_billed 
WHERE forum_traad.id = '5' 

从丹麦语(我想)到英语的翻译将是这样的:

SELECT *, forum_thread.content as thread_content, 
          forum_comments.content as comments_content, 
          forum_thread.user as thread_user, 
          forum_thread.id as thread_id, 
          users.profil_image as image 
FROM (forum_thread) 
    LEFT JOIN forum_comments 
    ON forum_comments.fk_forum_thread = forum_thread.id 
        LEFT JOIN users 
        ON forum_thread.user = users.profil_image 
WHERE forum_thread.id = '5' 

在最后一次加入你在图像列上加入用户表?尝试改为:

LEFT JOIN users
ON forum_comments.user = users.id

在这种情况下,您将获得所有在帖子中发表评论的人(包括主要海报,因为他的帖子是第一个?)

我不确定这是否是正确的解决方案(答案)。如果不在此发表您的评论,我们会尽力提供帮助。

相关问题