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

Daniel Bingham picture Daniel Bingham · Mar 21, 2011 · Viewed 16.9k times · Source

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;

Answer

Richard Parnaby-King picture Richard Parnaby-King · Mar 21, 2011

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')))
            )
    );