Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ZF2 WHRE句に副問い合わせ
<?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オブジェクト
$subselect = $sql->select();
$subselect->from('album');
$subselect->columns(array(
'latest_release' => new \Zend\Db\Sql\Predicate\Expression('MAX(album.release_date)')
));
//WHERE句に副問い合わせを指定
$select->where(array(
'release_date' => $subselect
));
//echo $select->getSqlString($adapter->getPlatform());
$statement = $sql->prepareStatementForSqlObject($select);
$resultSet = $statement->execute();
//SELECT `album`.* FROM `album` WHERE `release_date` = (SELECT MAX(album.release_date) AS `latest_release` FROM `album`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment