Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Select the MAX of a Column using Zend_Db_Table?

What is the easiest, simplest way to select the max of a column from a table using Zend_Db_Table? Basically, I just want to run this query in Zend:

SELECT MAX(id) AS maxID FROM myTable;
like image 417
Daniel Bingham Avatar asked Mar 21 '11 16:03

Daniel Bingham


2 Answers

You can run direct sql, using $db->query(); yours would simply be:

$db->query("SELECT MAX(id) AS maxID FROM myTable");

but if you want the object notation, then you'd do something like this:

$db->select()->from("myTable", array(new Zend_Db_Expr("MAX(id) AS maxID")));
like image 84
Glen Solsberry Avatar answered Sep 23 '22 16:09

Glen Solsberry


You need to use Zend_Db_Expr to use mysql functions:

return $this->fetchAll(
            $this->select()
                ->from($this, array(new Zend_Db_Expr('max(id) as maxId')))
            )
    );
like image 11
Richard Parnaby-King Avatar answered Sep 21 '22 16:09

Richard Parnaby-King