Search code examples
phpmysqlsqlzend-frameworkzend-db

Solely COUNT(*) from Zend_DB Select Statement (Subquery)


I'm trying to wrap a count(*) query around an existing Zend_Db select statement, but all I was able to get is:

SELECT `t`.*, COUNT(*) AS `TotalRecords` FROM (SELECT ....) AS `t`

However I like to get rid of the t.* as I only need the count(*).

This is my code so far:

$db = Zend_Registry::get('db');
$select = $dbmodel->getSomething(); //zend select object
$outterSelect = new Zend_Db_Select($db);
$outterSelect->from($select)->columns(array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));
echo $outterSelect->__toString();

Any help is appreciated!


Solution

  • You can simply write:

    $outterSelect->from($select, 'COUNT(*) as TotalRecords');