Skip to content

Instantly share code, notes, and snippets.

@matstani
Last active August 29, 2015 13:57
Show Gist options
  • Save matstani/9445604 to your computer and use it in GitHub Desktop.
Save matstani/9445604 to your computer and use it in GitHub Desktop.
ZF2 集計
<?php
$sm = $this->getServiceLocator();
$adapter = $sm->get('Zend\Db\Adapter\Adapter');
$sql = new \Zend\Db\Sql\Sql($adapter);
$select = $sql->select();
$select->from('album');
$select->columns(array(
'artist',
'latest_release' => new \Zend\Db\Sql\Predicate\Expression('MAX(album.release_date)')
));
$select->group(array(
'album.artist'
));
$select->having(array(
'MAX(album.release_date) >= ?' => date('Y-m-d', strtotime('-1 year'))
));
//echo $select->getSqlString($adapter->getPlatform());
$statement = $sql->prepareStatementForSqlObject($select);
$resultSet = $statement->execute();
//SELECT `album`.`artist` AS `artist`, MAX(album.release_date) AS `latest_release` FROM `album` GROUP BY `album`.`artist` HAVING MAX(album.release_date) >= '2013-03-09'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment