表连接:无法显示我想要的数据


Table join: unable to show the data what I want

如果我有一个这样的表:

ID | Title | Topic      | Summary    
1   |   A   | Technology | ...    
2  |   B   | Health     | ...    
3  |   C   | Sport      | ...

这是我的CI_Model:

function show($limit, $offset)
    {
        $this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');
        $this->db->from('document');
        $this->db->join('topic', 'topic.id_topic = document.id_topic');
        $this->db->limit($limit, $offset);
        $this->db->order_by('id', 'asc');
        return $this->db->get()->result();
    }

这是我的控制器:

            $docdata = $this->Trainingmodel->show($this->limit, $offset);
        ...
        $this->table->set_heading('ID', 'Title', 'Topic', 'Summary');
        foreach ($docdata as $doc)
        {
            $this->table->add_row($doc->id, $doc->title, $doc->topic, $doc->summary);                                                                                               
        }

显然,主题显示了它的id,而不是名称。例如:

ID | Title | Topic      | Summary    
1   |   A   | 1 | ...    
2  |   B   | 2  | ...    
3  |   C   | 3  | ...

我该怎么办?我想显示主题的名称,而不是主题的id。

看看你在其他答案中作为评论发布的表结构,我认为你需要select()中的topic.topicjoin()中的topic.id = document.id_topic -

$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');
$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');

可能是因为这个document.id_topic

$this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');

应该是document.topic还是document.name_topic呢?