22

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

4

4 回答 4

34

我在http://blog.abmeier.de/php/zf2-select-distinct上发现了这个非常有用的解决方案

$sql = new Sql($adapter);
$select = $sql->select();
$select->quantifier('DISTINCT');
于 2013-06-24T15:05:17.450 回答
19

在列选择中使用表达式。

$select = new Select('tablename');
$select->columns(array(new Expression('DISTINCT(id) as id')));
于 2013-03-12T16:16:10.337 回答
19

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

$sql->select()->quantifier(\Zend\Db\Sql\Select::QUANTIFIER_DISTINCT)
于 2013-09-29T20:33:59.157 回答
0

这对我来说效果最好。

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

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

于 2018-10-15T11:34:04.983 回答