1

i found some solutions for Zend\Db\Sql SQL aggregate functions, but they are not working for me :(

my old PDO Statement (works with "simple" PDO):

SELECT DISTINCT DATE_FORMAT(date, '%e') FROM termine WHERE MONTH(date) = ?

Solution for the first aggregate function:

$select->columns(array(new Expression("DISTINCT DATE_FORMAT(date, '%e')")));

But for second aggregate function i have no idea and cant find anything.

$select->where(array('MONTH(date)' => $month));

...is no solution, because it throws an error.

I know: one more table column (month) can be my solution ...but it is not good enough.

How can i use SQL aggregate functions as key in Zend\Db\Sql\Select()->where()?

4

1 回答 1

1

不知道这个答案有多好,因为我还没有测试过。但是, Predicate 类之一是Expression。我相信,哪个应该做你正在寻找的东西。然后您可以将 Predicate 传递给 where 方法(它接受数组、PredicateSets、Predicates 和 Where 类)。

还有可能使 Month() 成为列并使用 have 子句。例如:

$select->columns(array(
     new Expression("DISTINCT DATE_FORMAT(date, '%e')"),
     'month' => new Expression("MONTH(date)"),
  )
)->having(array('month' => $month));

我知道这很有效,因为我在几个查询中使用它。但是,使用 Expression prediate 可能是更好的方法。

于 2013-09-26T01:19:51.207 回答