createQueryBuilder('t'); return $qb ->join('t.customers', 'c') ->wh-6ren">
gpt4 book ai didi

mysql - 在 MySQL 中不使用 GROUP 获取 "#1111 - Invalid use of group function"

转载 作者:行者123 更新时间:2023-11-30 23:59:02 25 4
gpt4 key购买 nike

    $qb = $this->createQueryBuilder('t');

return $qb
->join('t.customers', 'c')
->where($qb->expr()->eq('t.user', $user->getId()))
->andWhere($qb->expr()->gt($qb->expr()->count('c'), 0))
->orderBy('t.name')->getQuery()->getResult();

上面的查询(Doctrine2 生成的一个)给我这个错误:

#1111 - Invalid use of group function

但奇怪的是我没有使用GROUP BY。非常感谢任何帮助,谢谢。

SELECT t0_.id AS id0,
t0_.slug AS slug1,
t0_.name AS name2,
t0_.description AS description3,
t0_.user_id AS user_id4
FROM tag t0_
INNER JOIN customers_tags c2_ ON t0_.id = c2_.tag_id
INNER JOIN customer c1_ ON c1_.id = c2_.customer_id
WHERE t0_.user_id = 1 AND COUNT(c1_.id) > 0
ORDER BY t0_.name ASC

最佳答案

您在不允许的 where 子句中使用聚合函数 count()

聚合函数的条件需要进入HAVING子句

....
WHERE t0_.user_id = 1
HAVING count(c1_.id) > 0

当然,您需要使用 GROUP BY 来获得正确的结果(尽管 MySQL 可能会让您不使用 GROUP BY 而逃脱 - 但结果是不可预测的)

关于mysql - 在 MySQL 中不使用 GROUP 获取 "#1111 - Invalid use of group function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com