How can I use the distinct
clause with Zend\Db\Sql\
?
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');
Use an expression in your column selection.
$select = new Select('tablename');
$select->columns(array(new Expression('DISTINCT(id) as id')));
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With