所以我一直在绞尽脑汁。在 Doctrine 中没有右连接的概念。我知道您可以像使用右连接一样使用左连接,但对于我的示例,我无法弄清楚这就是我在这里的原因。
我的例子:我在 Doctrine 中有一个实体,它与自身具有一对一的关系,称为“父级”。我试图让所有实体及其子项(如果它们存在)没有重复。
通过正确的加入,这很简单,因为我可以说:
SELECT parent.*, child.*
FROM table child
RIGHT JOIN table parent ON parent.id = child.parent_id
WHERE parent.parent_id is null;
但是使用左连接我返回的结果是我无法弄清楚 where 子句应该是什么来过滤掉它们。
所以我的问题是“学说是否有办法进行右连接”或“我怎样才能像右连接一样使用左连接操作”?
- -编辑 - -
你们所说的关于更改我从中选择的表格的顺序是正确的,但我使用的是 Doctrine,所以关系是子->父。这是我的教义查询:
我的实体:
/**
* @Entity
* @Table(name="entity")
*/
class Entity
{
...
/**
* @OneToOne(
* targetEntity="Entity",
* fetch="EAGER"
* )
* @JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
...
}
我的学说选择声明:
$em->createQueryBuilder()
->select(array('child', 'parent'))
->from('Entity', 'child')
->leftjoin('child.parent', 'parent')
->orderBy('parent.id','asc')
->getQuery()
->execute();
我不确定如何以及是否可以切换表格的顺序。我还尝试创建从实体到自身的另一种关系(就像我做父母的方式一样),但称它为孩子。但是当我用新的模式学说更新数据库时抛出了错误。
有任何想法吗?感谢您的快速回复!
---编辑2---
左连接sql和结果:
SELECT child.id, child.changed_timestamp, child.parent_entity_id, parent.id,
parent.changed_timestamp, parent.parent_entity_id
FROM content child
LEFT JOIN content parent ON child.parent_entity_id = parent.id
ORDER BY parent.id ASC
child_id child_timestamp parent_entity_id parent_id parent_timestamp parent_entity_id
1 8/16/12 20:29 NULL NULL NULL NULL
7 9/20/12 16:07 NULL NULL NULL NULL
8 8/17/12 16:08 NULL NULL NULL NULL
9 8/17/12 20:44 NULL NULL NULL NULL
10 8/17/12 21:03 NULL NULL NULL NULL
11 8/17/12 21:17 NULL NULL NULL NULL
194 9/19/12 9:58 NULL NULL NULL NULL
195 9/20/12 10:38 NULL NULL NULL NULL
196 9/19/12 11:58 NULL NULL NULL NULL
197 NULL 196 196 9/19/12 11:58 NULL
200 9/20/12 16:02 1 1 8/16/12 20:29 NULL
202 9/20/12 16:35 NULL NULL NULL NULL
204 9/21/12 8:41 NULL NULL NULL NULL
206 NULL 204 204 9/21/12 8:41 NULL
右连接结果:
SELECT child.id, child.changed_timestamp, child.parent_entity_id, parent.id,
parent.changed_timestamp, parent.parent_entity_id
FROM content child
RIGHT JOIN content parent ON child.parent_entity_id = parent.id
WHERE parent.parent_entity_id is null
ORDER BY parent.id ASC
child_id child_timestamp parent_entity_id parent_id parent_timestamp parent_entity_id
200 9/20/12 16:02 1 1 8/16/12 20:29 NULL
NULL NULL NULL 7 9/20/12 16:07 NULL
NULL NULL NULL 8 8/17/12 16:08 NULL
NULL NULL NULL 9 8/17/12 20:44 NULL
NULL NULL NULL 10 8/17/12 21:03 NULL
NULL NULL NULL 11 8/17/12 21:17 NULL
NULL NULL NULL 194 9/19/12 9:58 NULL
NULL NULL NULL 195 9/20/12 10:38 NULL
197 NULL 196 196 9/19/12 11:58 NULL
NULL NULL NULL 202 9/20/12 16:35 NULL
206 NULL 204 204 9/21/12 8:41 NULL
我想用正确的join sql来实现结果。唯一的父实体及其关联的子实体(如果它们存在),但我需要使用学说来实现它。再次感谢!