Hi,
I am doing a query to find the maximum value of a value in a column using the standard "SELECT MAX(column) FROM table WHERE 1" database query which is working.
However I am trying to get the value out of the result using:
$myResult = $db->query( "SELECT MAX(column) FROM table WHERE 1" );
$maxColumnValue = $myResult[0]->MAX(column);
I know the second row is wrong. What I want to know is what is the correct syntax for getting the MAX(column) result out of the database result object created from my query on the first line.
All suggestions much appreciated, thanks in advance
Kind regards,
Sam Clark
$maxColumnValue = $db->query('SELECT MAX(column) as max FROM table')->current()->max; I've fixed it....
I'm such a dullard... of course MySQL can do this for me :-)
$myResults = $db->query( "SELECT MAX(column) as maxColumn FROM table WHERE 1" );
Well, there you go for anyone else stuck
Posted By: zomborTry:
$maxColumnValue = $db->query('SELECT MAX(column) as max FROM table')->current()->max;
I never understood the point of MAX(), when this works just as well:
SELECT value FROM table ORDER BY value DESC LIMIT 0, 1
Said syntax also works with query builder:
$max = $this->db->select('value')->from('table')->orderby('value', 'desc')->limit(1)->get()->current()->value;
Posted By: dleavittAny interest in integrating MAX, MIN, etc into the core DB library? These are pretty standard active record methods imo.
Posted By: dleavittI think you may be a little hasty in your dismissal! There's a reason most frameworks include wrappers for these functions - in addition to being difficult to parse at first glance, shadowhand's method is much slower on unindexed columns than using MAX() would be.
Posted By: zomborYou can globally turn off escaping in SVN, it is a 2.2 feature. Use at your own risk.
$data = array( 'id' => 4, 'name' => 'edam', 'count' => 'count + 1');
$this->db->update( 'people', $data );
$data = array( 'id' => 4, 'name' => 'edam', 'count' => array( 'count + 1', false ) );
$this->db->update( 'people', $data );
Posted By: edam@zombor: intriguing! Can you give us a quick example of usage of the new interface?
It looks like you're new here. If you want to get involved, click one of these buttons!