Emmanuel Gauthier
Emmanuel Gauthier

Reputation: 551

How to use distinct in Zend Framework 2?

How can I use the distinct clause with Zend\Db\Sql\?

Upvotes: 23

Views: 14392

Answers (4)

Tristan Hudson
Tristan Hudson

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

Julian
Julian

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

Mihai
Mihai

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

Andrew
Andrew

Reputation: 12809

Use an expression in your column selection.

$select = new Select('tablename');
$select->columns(array(new Expression('DISTINCT(id) as id')));

Upvotes: 20

Related Questions