2

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!

1 Answer 1

5

You can simply write:

$outterSelect->from($select, 'COUNT(*) as TotalRecords');
Sign up to request clarification or add additional context in comments.

3 Comments

would it also be possible to get the result by only using $select. that means without $db?
If your mobel is extend Zend_Db_Table, you can do something like $model->select()->from->(...)
something like this $res = $doc->select()->from($select, 'COUNT(*) as rowcount')->fetchAll()->toArray(); doesn't work for me. fetchAll() is not known in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.