【问题标题】:how to add an offset and limit with join query?如何使用连接查询添加偏移量和限制?
【发布时间】:2016-02-24 06:23:44
【问题描述】:

我是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');
}

我想在上面的查询中添加偏移量和限制,谁能帮我修改它来完成?

提前致谢

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:
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');
}

【讨论】:

    【解决方案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');
    }
    

    【讨论】:

      【解决方案3】:

      我刚刚注意到查询构建器部分中的以下功能:

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

      【讨论】:

        猜你喜欢
        • 2014-02-21
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 2016-09-19
        • 2021-04-02
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        相关资源
        最近更新 更多