Pol*_*ino 0 mysql sql doctrine doctrine-orm
$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();
Run Code Online (Sandbox Code Playgroud)
上面的查询(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
Run Code Online (Sandbox Code Playgroud)
您count()在where子句中使用了一个不允许的聚合函数.
聚合函数的条件需要进入HAVING子句
....
WHERE t0_.user_id = 1
HAVING count(c1_.id) > 0
Run Code Online (Sandbox Code Playgroud)
当然,您需要使用GROUP BY来获得正确的结果(尽管MySQL可能会让您在不使用GROUP BY的情况下逃脱 - 但结果是不可预测的)