如何在Codeigniter中查询另一个查询的结果

时间:2015-03-05 11:58:19

标签: codeigniter model

我有一个小问题: 我创建了一个函数,其中包含一个连接多个表的查询,该函数返回查询结果。 我的问题是:我如何查询该函数的结果? 到目前为止,搜索到的解决方案都没有为我工作。 这是代码:

public  function manytomany(){
$this->db->select(*)
        ->from('tbl_users')
        ->join('tbl_users_cars','tbl_users.user_id = tbl_users_cars.user_id')
        ->join('tbl_cars','tbl_users_cars.car_id = tbl_cars.id')
        ->join('tbl_car_model','tbl_cars.car_model_id = tbl_car_model.car_model_id')
        ->order_by("user_id", "asc")
        ->group_by("user_id");
return $query = $this->db->get()->result();

如何在同一函数中查询结果?但功能之外? 谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定你想要它,但试试这个:

public  function manytomany() {

$query = $this->db->select(*)
        ->from('tbl_users')
        ->join('tbl_users_cars','tbl_users.user_id = tbl_users_cars.user_id')
        ->join('tbl_cars','tbl_users_cars.car_id = tbl_cars.id')
        ->join('tbl_car_model','tbl_cars.car_model_id = tbl_car_model.car_model_id')
        ->order_by("user_id", "asc")
        ->group_by("user_id");

$query = $this->db->get()->result();

return $query;
}
public function otherfunction()
{
$result_query = $this->manytomany();
}

答案 1 :(得分:0)

试试这个:

public  function manytomany(){
        $this->db->select(*)
                ->from('tbl_users')
                ->join('tbl_users_cars','tbl_users.user_id = tbl_users_cars.user_id')
                ->join('tbl_cars','tbl_users_cars.car_id = tbl_cars.id')
                ->join('tbl_car_model','tbl_cars.car_model_id = tbl_car_model.car_model_id')
                ->order_by("user_id", "asc")
                ->group_by("user_id");
        $sql = $this->db->get_compiled_select();
        $this->db->query('SELECT * FROM ($sql) as table WHERE ...');
    ...
    }