Question: In ZF1 it worked like this:

```
 $selectColumns = array(
   '*',
   'orders_total' => "(".$db->select()->from("orders", array("COUNT(*)"))->where("orders.parent_id=mytable.id").")",
 );
 $select = $db->select()->from('mytable', $selectColumns);

```

How to do this in the ZF2? Thanks.

Answer: 
Please try this.

```
$sql = new Sql($this->_adapter);
$mainSelect = $sql->select()->from('mytable');
$subQry = $sql->select()
        ->from('orders')
        ->columns(array('orderCount' => new \Zend\Db\Sql\Expression('COUNT(orders.id)')));
$mainSelect->columns(
        array(
            'id', 
            'orders_total' => new \Zend\Db\Sql\Expression('?', array($subQry)),
        )
);

$statement = $sql->prepareStatementForSqlObject($mainSelect);
$comments = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($comments);

return $resultSet->toArray();

```

Link: [ZF2 - subqueries](https://stackoverflow.com/questions/15742290/subquery-in-zend-framework2/15743270#15743270)
Without any more info you could try:

```
 $selectColumns = array(
    '*',
    'orders_total' => "(".$db->select()->from("orders", array("COUNT(*)"))->where("orders.parent_id", "mytable.id").")",
);
 $select = $db->select()->from('mytable', $selectColumns);

```

You will need to add at top with the use statements:

```
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

```