zend数据库查询限制无法按预期工作

时间:2012-04-11 04:20:06

标签: mysql zend-framework

我有一个查询从db读取杂志

$select = $db->select()
            ->from('tbl_magazine',array('magazine_id','magazine_name'))
            ->join('tbl_magazine_issue','tbl_magazine_issue.magazine_id = tbl_magazine.magazine_id',array(null))
            ->join('mst_publisher','mst_publisher.publisher_id = tbl_magazine.publisher_id',array(null))
            ->where('tbl_magazine.is_active =?',1)
            ->where('mst_publisher.is_active =?',1)
            ->where('tbl_magazine_issue.os_select =?',2)
            ->where('tbl_magazine_issue.publish_status = ?',1)
            ->where('curdate() <= DATE(tbl_magazine.validity_till)')
            ->group('tbl_magazine.magazine_id')
            ->limit($start,$per_page);

但是当我打印查询时,我看到一些像这样的东西

SELECT `tbl_magazine`.`magazine_id`, `tbl_magazine`.`magazine_name` 
FROM `tbl_magazine` 
INNER JOIN `tbl_magazine_issue` ON tbl_magazine_issue.magazine_id = tbl_magazine.magazine_id 
INNER JOIN `mst_publisher` ON mst_publisher.publisher_id = tbl_magazine.publisher_id 
WHERE (tbl_magazine.is_active =1) AND (mst_publisher.is_active =1) 
  AND (tbl_magazine_issue.os_select =2) AND (tbl_magazine_issue.publish_status = 1) 
  AND (curdate() <= DATE(tbl_magazine.validity_till)) 
GROUP BY `tbl_magazine`.`magazine_id` 
LIMIT 2147483647 OFFSET 8

但是我将$ start设置为0,将$ perpage设置为8

我正在寻找限制为"limit 0 ,8"

的查询

1 个答案:

答案 0 :(得分:5)

您没有正确使用limit

以下是Zend_Db_Select的功能定义。如您所见,第一个参数是count而不是offset

/**
 * Sets a limit count and offset to the query.
 *
 * @param int $count OPTIONAL The number of rows to return.
 * @param int $offset OPTIONAL Start returning after this many rows.
 * @return Zend_Db_Select This Zend_Db_Select object.
 */
public function limit($count = null, $offset = null)
{
    $this->_parts[self::LIMIT_COUNT]  = (int) $count;
    $this->_parts[self::LIMIT_OFFSET] = (int) $offset;
    return $this;
}

<小时/> 并且只是解释为什么你得到那些疯狂的值...下面是呈现SQL查询的脚本的一部分。因为您有偏移量,所以将计数设置为PHP_INT_MAX。

 if (!empty($this->_parts[self::LIMIT_OFFSET])) {
        $offset = (int) $this->_parts[self::LIMIT_OFFSET];
        $count = PHP_INT_MAX;
    }