Php MySQL在CodeIgniter中将同一表中的两个不同列连接两次

Php MySQL在CodeIgniter中将同一表中的两个不同列连接两次,php,mysql,sql,codeigniter,Php,Mysql,Sql,Codeigniter,我有announcements表,其中存储announcements.catid(类别id) Categories表Categories用于嵌套使用Categories.pid 我想做的是,在多维数组中一个接一个地嵌套类别 以下是我希望它如何位于突出显示部分内的图像: 代码如下: public function get_mixed( $limit = NULL, $offset = NULL ) { $this->db ->select('

我有
announcements
表,其中存储
announcements.catid
(类别id)

Categories表
Categories
用于嵌套使用
Categories.pid

我想做的是,在多维数组中一个接一个地嵌套类别

以下是我希望它如何位于突出显示部分内的图像:

代码如下:

public function get_mixed( $limit = NULL, $offset = NULL )
{
    $this->db
        ->select('
            announcements.id,
            announcements.catid,
            announcements.uid,
            categories.title AS section,
            categories.title AS category,
            announcements.title,
            announcements.text,
            announcements.views,
            announcements.created,
            announcements.modified,
            announcements.pubdate
        ')
        ->join('categories', 'announcements.catid = categories.id', 'left')
        ->join('categories as section', 'categories.pid = section.id', 'left')
        ->order_by('created DESC, '.$this->_order_by);

    return $this->db->get( $this->_table_name, $limit, $offset )->result();
}
我首先尝试创建一个
别名,但得到了相同的结果


也许有更好的方法吗?

如果将一个
类别
联接别名为
,则需要将该
类别
表中的任何字段引用为
节。[field]
(例如,
节.title

由于不知道您到底想要什么,我对您修改后的查询的最佳猜测是:

$this->db
    ->select('
        announcements.id,
        announcements.catid,
        announcements.uid,
        section.title AS section,
        categories.title AS category,
        announcements.title,
        announcements.text,
        announcements.views,
        announcements.created,
        announcements.modified,
        announcements.pubdate
    ')
    ->join('categories', 'announcements.catid = categories.id', 'left')
    ->join('categories as section', 'categories.pid = section.id', 'left')
    ->order_by('created DESC, '.$this->_order_by);

正是我想要的,我忘记了在SELECT中引用别名。非常感谢。