Search code examples
zend-frameworkzend-db-tablezend-db-select

Zend Framework: Select the greatest number from table columns


How can I select the greatest number from table columns, exp. that is my table:

id|name|views|
1|test|42|
2|test1|89|
3|test2|4|
4|test3|35|

I need to select all values for row id 2, because views is most greatest number from other views? I try this, but don't work:

$q = $this->select()->from($this->_name, array(new Zend_Db_Expr('MAX(views)'), 'id', 'name'))->order('name DESC')->limit(1)->group('name');

return $this->fetchRow($q);

Solution

  • Try...

    $q = $this->select()
            ->from($this->_name, array('id', 'name'))
            ->order('views DESC')
            ->limit(1);
    return $this->fetchRow($q);
    

    Hint:

    Apply ORDER BY views DESC, and then LIMIT 1