ZF2-424: Sql query builder doesn't support distinct


The Sql object query builder doesn't support the distinct keyword, which was available in ZF1.


It does indeed, you can pass an expression as a column name ($select is an instance of Zend\Db\Sql\Select)

    'aid' => new Zend\Db\Sql\Expression('DISTINCT artist_id')

If you have to ensure that the column is properly quoted for future abstraction purposes:

    'aid' => new Zend\Db\Sql\Expression(
       'DISTINCT ?',

This doesn't feel very obvious and causes an error if the distinct expression doesn't come first (certainly using MySQL). How can I take a base select query and later in another method add a join and make it return distinct rows? In my specific case I have a base query that selects orders, I might modify this with a join against the order items when I want to only select orders that have a certain item. In ZF1 you would simply do the join and call the distinct method.

You make an excellent point. I'll work on this soon.

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2509