2

我有以下 dql SELECT 查询,该查询有效:

SELECT q AS question, AVG(r.score) AS average
  FROM MyBundle:Question q
  JOIN q.ratings r
  WHERE q.deleted IS NULL
  GROUP BY q.id
  ORDER BY q.id ASC

AVG(r.score)如果是 0 或 null ,我想做的是让它返回 0 。我只是不知道该怎么做。是否可以执行以下操作:

SELECT q AS question, (AVG(r.score) OR 0) AS average FROM....
4

3 回答 3

2

由于不能在WHERE子句中使用聚合函数,因此必须HAVING改用。这应该够了吧:

SELECT q AS question, AVG(r.score) AS average
  FROM MyBundle:Question q
  JOIN q.ratings r
  WHERE q.deleted IS NULL
  GROUP BY q.id
  HAVING average IN (0, NULL)
  ORDER BY q.id ASC
于 2013-06-12T22:28:36.667 回答
0

您是否正在寻找 mysql IFNULL的替代品来替代学说查询构建器。看看这个问题

于 2013-06-13T12:15:40.033 回答
0

将我的 (INNER) JOIN 更改为 LEFT OUTER JOIN 给了我想要的结果。

于 2013-06-13T18:31:01.013 回答