php – 如何分组更多和哪些,或者在Doctrine中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何分组更多和哪些,或者在Doctrine中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1696字,纯文字阅读大概需要3分钟。
内容图文
![php – 如何分组更多和哪些,或者在Doctrine中](/upload/InfoBanner/zyjiaocheng/751/5578360117f74a99b9c4682a28740a06.jpg)
SELECT * FROM dg
WHERE
( a < 1 AND b > 1)
OR ( a > 1 AND (
(c = 3 AND B < 2)
or (c = 4 AND B < 5 ))
)
我不确定如何正确分组更多和哪??些和哪些.我找到了一个更多AND组的例子,但没有OR的例子.
对于exp.其中a = 1 AND(a> 1或b = 2)AND(a> 1 OR c = 2)工作查询是:
public function myQuery()
{
return $this->createQueryBuilder( 'dg' )
->where("a = 1")
->andWhere("a > 1 OR b = 2")
->andWhere("a > 1 OR c = 3")
->getQuery()
->getResult()
;
}
如何在Doctrine2中使用我的SELECT来创建查询生成器?
解决方法:
对于or和/和之类的分组和层次结构,您可以使用querybuilder的 – > expr()方法链接和/或分别链接到您的QueryBuilder实例上的 – > andX()和 – > orX().您可以在此处查看更多信息:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#the-expr-class
基本上你会得到类似下面的内容来翻译你的第二个陈述:
// In this example I'll be assuming that 'dg' is an entity
// and that 'a', 'b' and 'c' are its attributes
// since, remember, Doctrine is designed specifically for using entities
// and make abstraction of the whole table model in your database
// First we'll create your QueryBuilder instance $qb
$qb = $this->createQueryBuilder('dg');
// Then we add our statements to the QueryBuilder instance
$qb
->where($qb->eq('dg.a', 1))
->andWhere($qb->expr()->orX(
$qb->expr()->gt('dg.a', 1),
$qb->expr()->eq('dg.b', 2)
))
->andWhere($qb->expr()->orX(
$qb->expr()->gt('dg.a', 1),
$qb->expr()->eq('dg.c', 3)
))
;
// Now you can use the QueryBuilder instance to, for instance,
// have it do getResult (which in this case will return an array of 'dg' entities)
return $qb->getQuery()->getResult();
您也可以将orX()和andX()放入其他orX()和andX()中,并且可以在andX()和orX()中添加任意数量的条件,创建非常复杂的查询.
玩得开心 :)
内容总结
以上是互联网集市为您收集整理的php – 如何分组更多和哪些,或者在Doctrine中全部内容,希望文章能够帮你解决php – 如何分组更多和哪些,或者在Doctrine中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。