繁体   English   中英

Codeigniter-join()仅返回一个“第一张表”

[英]Codeigniter - join() returns only one “first table”

我在模型中与codeigniter的活动记录存在冲突。

问题是我的查询仅返回一个表,我的联接表丢失了?

    $this->db->select("page.ID, page.TITLE, category.TITLE");
    $this->db->from('page');
    $this->db->join('category','page.ID_CATEGORY = category.ID');
    $query = $this->db->get();

    return $query->result_array();

您需要为查询提供唯一的别名,例如数组索引是唯一的,您的查询中的同一索引上不能有两个值TITLE将成为具有记录的数组的索引,因此请尝试下面的查询,您在数组形式中的当前结果将看起来像喜欢

array(
[0] => array(
['ID'] => 1,
['title'] => test page
)
[1] => ...
)

因为两个连接表中的列名标题都相同,所以不能像下面的数组那样在同一索引上有2个值

array(
[0] => array(
['ID'] => 1,
['title'] => test page,
['title'] => test cat /* wrong index */
)
[1] => ...
)

询问

$this->db->select("page.ID, page.TITLE AS page_title, category.TITLE AS cat_title");
$this->db->from('page');
$this->db->join('category','page.ID_CATEGORY = category.ID');
$query = $this->db->get();

所以结果数组看起来像

array(
[0] => array(
['ID'] => 1,
['page_title'] => test page,
['cat_title'] => test cat,
)
[1] => ...
)

尝试

return $query->result();

代替

return $query->result_array();

或尝试这种方式:

$this->db
        ->select('ID')
        ->from('table2');


$subquery = $this->db->_compile_select();

$this->db->_reset_select(); 

$query  =       $this->db
                    ->select('t1.name')
                    ->from('table1 t1 ')
                    ->join("($subquery)  t2","t2.id = t1.t2_id")
                    ->get('table1 t1');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM