CodeIgniter活动记录无限制偏移?

时间:2012-12-05 23:00:05

标签: php mysql codeigniter sql-limit

我有两个变量$take(限制)和$skip(偏移量),它们是mysql中限制子句的值。

现在Code Igniter向后执行限制条款。

E.g。

$this->db->limit(5,10)会在MYSQL中生成LIMIT 10, 5

无论如何,我很难理解如何根据设置的两个值调用limit函数。

基本上,

如果他们都设置了我想打电话

$this->db->limit($take, $skip)

如果只设置了$take,我想调用

$this->db->limit($take)

但如果只有$skip is set, e.g. if $ skip = 10`我会打电话给那么我想要显示所有行,除了前10行。

我打电话

$this->db->limit(null, $skip)
$this->db->limit(0, $skip)
$this->db->limit(false, $skip)
完全不同的东西?

3 个答案:

答案 0 :(得分:5)

我认为您通过回答我们自己的问题找到了您正在寻找的答案但是如果您想使用codeigniter来获取除前10个之外的所有行,那么您将需要执行类似的操作(我还没有测试它) :

$this->db->limit(18446744073709551615, 10)

根据Mysql Documentation

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter.

答案 1 :(得分:1)

结果显示Code Igniter有一个未记录的offset函数。

/**
* Sets the OFFSET value
*
* @param    integer the offset value
* @return   object
*/
public function offset($offset)
{
    $this->ar_offset = $offset;
    return $this;
}

所以只需调用$this->db->offset($skip)就行了!

答案 2 :(得分:0)

This是{ignister 3的有效记录中的offset功能的正式文档。

  

offset($ offset)     参数:     $ offset(int)–要跳过的行数     返回:
    CI_DB_query_builder实例(方法链接)

     

返回类型:
    CI_DB_query_builder

     

向查询添加OFFSET子句。