Reputation: 551
How can I use the distinct
clause with Zend\Db\Sql\
?
Upvotes: 23
Views: 14392
Reputation: 151
This worked best for me.
$select = $this->select()
->distinct()
->where('user_id = ?', $user_id);
http://webphplearn.com/blog/blogdetail/Distinct_in_Zendframework2
Upvotes: 0
Reputation: 9130
While Mihai Dobre's answer is correct, I think you should use the constants provided by the framework instead of using a string literal. This will make your code more future-proof.
$sql->select()->quantifier(\Zend\Db\Sql\Select::QUANTIFIER_DISTINCT)
Upvotes: 20
Reputation: 366
I found this very usefull solution on http://blog.abmeier.de/php/zf2-select-distinct
$sql = new Sql($adapter);
$select = $sql->select();
$select->quantifier('DISTINCT');
Upvotes: 35
Reputation: 12809
Use an expression in your column selection.
$select = new Select('tablename');
$select->columns(array(new Expression('DISTINCT(id) as id')));
Upvotes: 20