我正在研究一个项目(Symfony2),它有两个具有多对多ORM连接的实体。 现在,我对这个DQL查询感到很生气:
SELECT p, COUNT(s.id) AS countSup FROM AapFrontendBundle:Paper p LEFT JOIN p.supervisors s WHERE (countSup = 0) GROUP BY p.id
使用SQL输出:
SELECT p0_.id AS id0,
p0_.title AS title1,
p0_.description AS description2,
p0_.research_questions AS research_questions3,
p0_.basic_literature AS basic_literature4,
p0_.student AS student5, p0_.status AS status6,
p0_.category AS category7,
p0_.type AS type8,
p0_.hash AS hash9,
COUNT(u1_.id) AS sclr10,
p0_.insertBy AS insertBy11
FROM paper p0_
LEFT JOIN supervisors s2_
ON p0_.id = s2_.paperId
LEFT JOIN user u1_
ON u1_.id = s2_.userId
WHERE (sclr10 = 0) GROUP BY p0_.id
收到错误消息
Column not found: Unknown column 'sclr10' in 'where clause'
表“主管”用于论文和用户之间的多对多关系。 基本上,我想在paper-repository中使用这个查询来查找所有没有链接用户作为主管的纸质实体。
答案 0 :(得分:1)
您可以使用GROUP BY
/ HAVING
组合:
SELECT ...
FROM paper p0_
LEFT JOIN supervisors s2_
ON p0_.id = s2_.paperId
LEFT JOIN user u1_
ON u1_.id = s2_.userId
GROUP BY p0_.id
HAVING COUNT(u1_.id) = 0