gpt4 book ai didi

subquery - Doctrine 2 子查询

转载 作者:行者123 更新时间:2023-12-04 08:37:55 29 4
gpt4 key购买 nike

我想使用查询构建器实现子查询,但我不理解语法。我正在处理一个位置表,其中的条目可以是城市、州或邮政编码,具体取决于位置类型集。我想获取处于某个州的所有位置,并取出任何城市类型且人口低于一定数量的位置。

$qb->select('l')
->from('Entity\Location', 'l')
->where('l.state = :state')
->setParameter('state', 'UT')
->andWhere('...don't know what to put here');

在andWhere我基本上需要说

and where id not in (select id from location where location_type = 1 and population < 1000)



更新 :我可以用直接的 DQL 做到这一点,但很高兴看到如何使用查询构建器来做到这一点。
$qb->andWhere('l.id NOT IN (SELECT l2.id FROM Entity\Location AS l2 WHERE l2.location_type = 1 AND l2.population < 1000)');

最佳答案

在Doctrine的文档中,我发现了这一点:

// Example - $qb->expr()->in('u.id', array(1, 2, 3))
// Make sure that you do NOT use something similar to $qb->expr()->in('value', array('stringvalue')) as this will cause Doctrine to throw an Exception.
// Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above)
public function in($x, $y); // Returns Expr\Func instance

// Example - $qb->expr()->notIn('u.id', '2')
public function notIn($x, $y); // Returns Expr\Func instance

应该可以在这个函数中放置一个子查询。我自己从未使用过它,但根据文档,它应该是这样的。
$qb->select('l')
->from('Entity\Location', 'l')
->where('l.state = :state')
->setParameter('state', 'UT')
->andWhere($qb->expr()->notIn('u.id',
$qb->select('l2.id')
->from('Entity\Location', 'l2')
->where(l2.location_type = ?1 AND l2.population < ?2)
->setParameters(array(1=> 1, 2 => 1000))
));

我不是 100% 确定上面的例子是正确的,但试一试。

关于subquery - Doctrine 2 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5941963/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com