http://localhost/kohana/index.php/guide/database/query/builder#aggregate-functions
Table Schema
============
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `example` (
`Name` varchar(100) NOT NULL,
`id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=latin1;
INSERT INTO `example` (`Name`, `id`) VALUES
('Robeul', 26),
('Asma', 27),
('Joy', 28),
('Tanvir', 29),
('Dilir', 48);
ALTER TABLE `example`
ADD PRIMARY KEY (`id`);
ALTER TABLE `example`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=49;
Table Schema
============
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `example` (
`Name` varchar(100) NOT NULL,
`id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=latin1;
INSERT INTO `example` (`Name`, `id`) VALUES
('Robeul', 26),
('Asma', 27),
('Joy', 28),
('Tanvir', 29),
('Dilir', 48);
ALTER TABLE `example`
ADD PRIMARY KEY (`id`);
ALTER TABLE `example`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=49;
----------------
Model
=====
<?php defined('SYSPATH') or die('No Direct Access');
class Model_Example extends ORM
{
protected $_table_name = 'example';
}
Controller
==========
public function action_getMax()
{
$exampleTable=ORM::factory('example');
$m=$exampleTable->select(array(DB::expr('MAX(id)'), 'max_id'))->find();
echo $m->max_id;
}
Output: 48
Note:
Generally, if the name of the model class is 'Model_Example', there needs to be a table named 'examples' in your database. But, since we chose to name the table as 'example' (we skipped the 's'), we need to make a declaration: protected $_table_name = 'example';
No comments:
Post a Comment