我想在Zend Framework 2中用select对象实现一个语句,如下所示:
select count(*) as total from ( select id from table group by name) a;
我的代码是:
$sub = $adapter->select()->from('table')->group('name');
$sql = $adapter->select()
->columns('total'->new Expression('count(*)'))
->from(array('a'=>$sub));
当我执行查询时,浏览器总是告诉我'$ table必须是字符串,数组或TableIdentifier的实例'。
感谢您的帮助!
答案 0 :(得分:3)
最后,我自己解决了这个问题。
$select1 = new \Zend\Db\Sql\Select();
$select1->from('table')->columns('id')->group('name');
$select2 = new \Zend\Db\Sql\Select();
$select2->columns('total'->new Expression('count(*)'))
->from(array('a'=>$select1));