- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如何使用 Zend\Db\TableGateway
获取 SQL_CALC_FOUND_ROWS
,而不使用原始 SQL 的直接低级查询?
class ProductTable {
protected $tableGateway;
/**
* Set database gateway
*
* @param TableGateway $tableGateway - database connection
* @return void
*/
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
}
/**
* Fetch all products
*
* @param integer $page - page of records
* @param integer $perpage - records per page
* @return void
*/
public function fetchAll($page = 1, $perpage = 18) {
return $this->tableGateway->select(function (Select $select) use ($page, $perpage) {
$select
->limit($perpage)
->offset(($page - 1) * $perpage);
});
}
}
我希望获得 fetchAll
中使用的同一查询中的记录总数。
最佳答案
看起来 Zend Framework 2.1.4 支持指定量词。这使您能够在选择对象中使用 SQL_CALC_FOUND_ROWS。我确实发现很难解决的一件事是,如果您没有指定表,Zend 的 Zend\Db\Sql\Select 类将不会为您生成正确的 SQL。这在执行后续选择以检索 FOUND_ROWS() 时成为问题。我已经在下面更新了您的代码以包含我将使用的内容。我已将我的项目实现合并到您的代码中,所以如果某些地方不起作用,可能是因为我输错了某些东西,但总的来说它对我有用(不像我想要的那样理想)。
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
class ProductTable {
protected $tableGateway;
/**
* Set database gateway
*
* @param TableGateway $tableGateway - database connection
* @return void
*/
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
}
/**
* Fetch all products
*
* @param integer $page - page of records
* @param integer $perpage - records per page
* @return void
*/
public function fetchAll($page = 1, $perpage = 18) {
$result = $this->tableGateway->select(function (Select $select) use ($page, $perpage) {
$select
->quantifier(new Expression('SQL_CALC_FOUND_ROWS'))
->limit($perpage)
->offset(($page - 1) * $perpage);
});
/* retrieve the sql object from the table gateway */
$sql = $this->tableGateway->getSql();
/* create an empty select statement passing in some random non-empty string as the table. need this because Zend select statement will
generate an empty SQL if the table is empty. */
$select = new Select(' ');
/* update the select statement specification so that we don't incorporate the FROM clause */
$select->setSpecification(Select::SELECT, array(
'SELECT %1$s' => array(
array(1 => '%1$s', 2 => '%1$s AS %2$s', 'combinedby' => ', '),
null
)
));
/* specify the column */
$select->columns(array(
'total' => new Expression("FOUND_ROWS()")
));
/* execute the select and extract the total */
$statement = $sql->prepareStatementForSqlObject($select);
$result2 = $statement->execute();
$row = $result2->current();
$total = $row['total']';
/* TODO: need to do something with the total? */
return $result;
}
关于mysql - 如何在 Zend\Db\TableGateway 中使用 SQL_CALC_FOUND_ROWS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14156151/
$query->select('SQL_CALC_FOUND_ROWS i.title,i.fulltext,i.shorttext'); $query->from('#__items
我们开始使用基于语句的复制,发现它破坏了 SQL_CALC_FOUND_ROWS 和 FOUND_ROWS(),我正在寻找解决方法。 2007 年错误报告中的评论建议使用 SELECT SQL_CAL
所以我执行查询: SELECT SQL_CALC_FOUND_ROWS * FROM table l LIMIT 10, 20; 从表中返回 20 行,表中共有 553 行 然后我立即执行SELECT
我正在处理使用 SQL_CALC_FOUND_ROWS() 的分页代码,但是当我将每页的结果限制为小于总结果的数字时,“SELECT FOUND_ROWS() as total”将返回每页的元素数,并
如何获取特定查询的总行数并将查询结果限制为 10 行。例如。我有一个名为样本的表。它有 400 行。在运行像 where name = "%Sam%"这样的查询时,它返回 213 行。现在我只取前 1
我使用嵌套的 MySQL 查询来获取用户帖子,当我将 SQL_CALC_FOUND_ROWS 放入子查询时,MySQL 返回错误:Incorrect usage/placement of 'SQL_C
我发现 MySQL 返回一个 Incorrect usage/placement of 'SQL_CALC_FOUND_ROWS' 如果我将 SQL_CALC_FOUND_ROWS 放入子查询中会出错
所以我有一个表,其中有超过 500 万行。当我使用 SQL_CALC_FOUND_ROWS 时,查询永远挂起。当我把它拿出来时,查询在一秒钟内执行,限制为 25。我的问题是出于分页原因,是否有替代方法
我目前正在摸索如何使用准备好的语句实现 SQL_CALC_FOUND_ROWS。 我正在编写一个分页类,显然我想向查询添加 LIMIT,但也想知道总行数是多少。 这是相关类(class)的示例。 $q
我想删除此查询返回的记录: SELECT SQL_CALC_FOUND_ROWS tag, COUNT( tag ) AS count FROM _tags GROUP BY tag HAVING c
我正在做一个简单的 MySQL 查询来计算查询返回的行数,而没有 LIMIT 子句的影响。我将 Active Records 与 Codeigniter PHP 框架结合使用。 问题:使用SQL_CA
我正在尝试从数据库进行分页,到目前为止我发现如果您想使用限制,您应该使用 SQL_CALC_FOUND_ROWS。 但是,在 Java 中,我在理解如何调用它时遇到了一些问题。 我可以在一个 prep
我有一个查询,它获取一个结果,对其进行限制,然后对有限的结果进行排序。像这样的东西: (SELECT SQL_CALC_FOUND_ROWS * FROM table ... ORDER BY sc
我在 MySQL、InnoDB 中使用 SQL_CALC_FOUND_ROWS 时遇到一些问题。首先让我展示一个简单的数据库架构来解释正在发生的事情...... Database Schema 媒体表
我正在使用名为 feedwordpress 的 WordPress 插件为了在 WordPress 上运行一个类似行星的网站(参见 here )。 这个插件很棒,除了一件事之外 - 它每周左右都会占用
根据这个答案:here The use of SQL_CALC_FOUND_ROWS is not subject to race conditions. The result of the SELE
我在 centos 6 机器上使用 MySQL 5.6(mysql Ver 14.14 Distrib 5.6.44,用于使用 EditLine 包装器的 Linux (i686)) 我有一个包含多个
我正在尝试为查询全文索引表的结果添加分页。这是查询: 普通查询 SELECT *,MATCH(title) AGAINST ("+samsung +galaxy +s3" IN BOOLEAN MOD
我在使用 MYSQL 时遇到问题。 SQL_CALC_FOUND_ROWS 和 FOUND_ROWS() 工作不正常。 问题: 第一次总返回1 第二次总数返回正确的3,500 名 MYSQL版本: m
这是我所知道的一种方式。请建议任何其他??? SELECT SQL_CALC_FOUND_ROWS * FROM Table; SELECT FOUND_ROWS(); 最佳答案 你可以使用(它会更快
我是一名优秀的程序员,十分优秀!