如何在Zend Framework 2中使用distinct?

时间:2013-03-11 20:34:23

标签: zend-framework2 zend-db zend-db-select

如何将distinct子句与Zend\Db\Sql\一起使用?

4 个答案:

答案 0 :(得分:30)

我在http://blog.abmeier.de/php/zf2-select-distinct

上找到了这个非常有用的解决方案
$sql = new Sql($adapter);
$select = $sql->select();
$select->quantifier('DISTINCT');

答案 1 :(得分:19)

在列选择中使用表达式。

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

答案 2 :(得分:18)

虽然Mihai Dobre的回答是正确的,但我认为您应该使用框架提供的常量而不是使用字符串文字。这将使您的代码更加面向未来。

$sql->select()->quantifier(\Zend\Db\Sql\Select::QUANTIFIER_DISTINCT)

答案 3 :(得分:0)

这对我来说效果最好。

$select = $this->select()
          ->distinct()
          ->where('user_id = ?', $user_id);

http://webphplearn.com/blog/blogdetail/Distinct_in_Zendframework2