【发布时间】:2014-06-19 14:17:03
【问题描述】:
我正在使用 Symfony 2 和学说。我有 4 个实体,CustomPage、Tag、CustomPageTag、Comment。
每个 CustomPage 可以有许多标签和许多 cmets。 我想选择一个带有特定标签的 customPage(包含所有 cmets)。
示例:我想选择带有 'tag1' AND 'tag2' 的 customPage
我这样做了,
SELECT CUSTOMPAGE, TAG, CUSTOMPAGE_TAG, COMMENT
FROM CustomBundle:CustomPage CUSTOMPAGE
JOIN CUSTOMPAGE.comments COMMENT
JOIN CUSTOMPAGE.tags CUSTOMPAGE_TAG
JOIN CUSTOMPAGE_TAG.tag TAG
WHERE TAG.name IN ('tag1','tag2')
GROUP BY CUSTOMPAGE.id
HAVING COUNT(DISTINCT CUSTOMPAGE_TAG.name) = 2
效果很好,但问题是它只返回一条评论(我想要全部)。
【问题讨论】:
-
你尝试左加入三个表而不是加入?
-
不,一样!它返回了良好的结果,但没有评论!
标签: mysql sql symfony doctrine