SQL / Doctrine:左连接问题 [英] SQL / Doctrine : Left Join problem
问题描述
目前这个功能有效:它显示一个特定的游戏,有多少个作业。
问题:如果没有工作,游戏不会出现在列表中。
如果没有附加工作,如何显示游戏?
谢谢
public function getWithGames()
{
$ q = $ this-> createQuery('c')
- > leftJoin('c.stJob j ')
- > where('j.expires_at>?',date('Ymd h:i:s',time())
- > addOrderBy('c.name' );
$ q-> andWhere('j.is_activated =?',1);
$ q-> andWhere('j.is_public =?',1);
return $ q-> execute();
}
你的条件应该是LEFT JOIN ... ON子句。
$ q = $ this-> createQuery('c')
- > leftJoin('c.stJob j WITH j.expires_at>?AND j.is_activated = 1 AND j.is_public = 1',date('Ymd h:i:s',time()))
- > addOrderBy('c.name');
将条件放在 ON
子句(as对 WHERE
)表示它们专门应用于 JOIN
。如果没有行满足这些条件,则没有加入 - 这就是您在这种情况下所需要的。将它们放在 WHERE
中表示 结果 行必须满足这些条件。显然,如果没有加入,则您无法满足 >
Currently this function works : it displays for a specific game, how many jobs there are.
The problem : If there is no job, the game does not appear on the list.
How to display the game even if there is no job attached ?
Thanks
public function getWithGames()
{
$q = $this->createQuery('c')
->leftJoin('c.stJob j')
->where('j.expires_at > ?', date('Y-m-d h:i:s', time()))
->addOrderBy('c.name');
$q->andWhere('j.is_activated = ?', 1);
$q->andWhere('j.is_public = ?', 1);
return $q->execute();
}
Your conditions should be part of the LEFT JOIN ... ON clause.
$q = $this->createQuery('c')
->leftJoin('c.stJob j WITH j.expires_at > ? AND j.is_activated = 1 AND j.is_public = 1', date('Y-m-d h:i:s', time()))
->addOrderBy('c.name');
Putting conditions in the ON
clause (as opposed to the WHERE
) indicates that they apply specifically to the JOIN
. If no rows satisfy those conditions, there is no join — and that's just what you want in this case. Putting them in the WHERE
indicates that the result rows must satisfy those conditions. And obviously if there was no join, you can't satisfy any conditions about the j
table.
这篇关于SQL / Doctrine:左连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!