开发者

Doctrine - filter by foreign agregate value

how can i use result of Agregate function in where in Doctrine?

For example i want to know user with silly much numbers.

SELECT  u.name, COUNT(p.id) AS users_phonenumber_count
FROM    users u

    INNER JOIN phonenumbers p ON p.user_id = u.id
WHERE
    users_phonenumber_count > 10
GROUP BY
    u.id

How can i access the u开发者_开发知识库sers_phonenumber_count in where in Dql?


You need to using HAVING, not WHERE. Also, you need to group by something that's actually in your query, in this case u.name. Assuming u.name is unique - if not, you need to group by both u.id and u.name.

SELECT  u.name, COUNT(*) AS users_phonenumber_count
FROM    users u

    INNER JOIN phonenumbers p ON p.user_id = u.id
GROUP BY
    u.name
HAVING
    count(*) > 10


I can't remember exact details of how I've done this, but remember that it had to use "HAVING"

This is because of the way SQL works, not specifically doctrine - WHERE can't compare computed values, you have to use having (or you could do WHERE COUNT(something) < something )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜