如何使用连接查询添加偏移和限制?

时间:2016-02-24 06:23:45

标签: php mysql codeigniter

我是codeigniter的新手,我已完成以下代码

public function members($limit, $offset)
{
$this->load->model('Management/Member_model', 'member');
    $this->db->reset_query();
    $this->db->select('*');
    $this->db->from($this->member->table);
    $this->db->join('ib_communities_dtl','ib_communities_dtl.dtl_user_id='.$this->member->table.'.user_id','inner');
    $this->db->join('ib_communities','ib_communities_dtl.dtl_community_id=ib_communities.community_id','inner');
    $this->db->where('ib_communities.community_id', $this->community_id);

  return $this->db->get()->result('Member_model');
}

我想为上述查询添加偏移和限制,任何人都可以帮我修改它以实现这一目标吗?

提前致谢

3 个答案:

答案 0 :(得分:4)

public function members($limit, $offset)
{
$this->load->model('Management/Member_model', 'member');
    $this->db->reset_query();
    $this->db->select('*');
    $this->db->from($this->member->table);
    $this->db->join('ib_communities_dtl','ib_communities_dtl.dtl_user_id='.$this->member->table.'.user_id','inner');
    $this->db->join('ib_communities','ib_communities_dtl.dtl_community_id=ib_communities.community_id','inner');
    $this->db->where('ib_communities.community_id', $this->community_id);
$this->db->limit($limit, $offset);

  return $this->db->get()->result('Member_model');
}

答案 1 :(得分:2)

您可以在CI limit函数中添加偏移量和限制为:

$this->db->limit(LIMIT,OFFSET);

示例:

public function members($limit, $offset)
{
    $this->load->model('Management/Member_model', 'member');
    $this->db->reset_query();
    $this->db->select('*');
    $this->db->from($this->member->table);
    $this->db->join('ib_communities_dtl','ib_communities_dtl.dtl_user_id='.$this->member->table.'.user_id','inner');
    $this->db->join('ib_communities','ib_communities_dtl.dtl_community_id=ib_communities.community_id','inner');
    $this->db->where('ib_communities.community_id', $this->community_id);
    $this->db->limit($limit,$offset);  //CHANGED
    return $this->db->get()->result('Member_model');
}

答案 2 :(得分:1)

我刚刚在查询构建器部分中注意到以下函数:

$this->db->limit(10, 20); // specifying the limit and the offset