在Zend_Db_Table_Abstract上,我使用以下代码来获取select结果的计数:
$this->setTableName('visitors');
$select = $this->select()
->from('visitors')
->columns(array('total' => new Zend_Db_Expr('COUNT(*)')))
->where('...');
$visits = $this->_fetch($select);有没有更好的方法,也就是,直接返回计数。这将返回数组中的其他一些数据...我只想知道结果的数量。在straight中,等效项是select count(mycol) from visitors where ....
发布于 2011-09-13 00:43:17
这是未经测试的,但应该可以帮助您入门:
$results = $this->getAdapter()
->query("SELECT COUNT(*) AS total FROM visitors WHERE ...")
->fetchAll();
$visits = $results[0]["total"];您不需要对每个查询都使用Table/Select接口。
更新:来自@SMka的评论+1,因为它指出这可以更简单:
$visits = $this->getAdapter()
->fetchOne("SELECT COUNT(*) AS total FROM visitors WHERE ...");发布于 2011-09-13 00:46:15
$count = $this->select()->from($this,'COUNT(*)')->query()->fetchColumn();发布于 2011-09-13 00:44:26
http://framework.zend.com/manual/en/zend.db.adapter.html
请参阅fetchOne
$this->setTableName('visitors');
$select = $this->select()
->from('visitors')
->columns(array('total' => new Zend_Db_Expr('COUNT(*)')))
->where('...');
$count = $this->getAdapter()->fetchOne($select);https://stackoverflow.com/questions/7391151
复制相似问题