查询EXISTS for Doctrine Symfony2 [英] Query with EXISTS for Doctrine Symfony2

查看:126
本文介绍了查询EXISTS for Doctrine Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Query Builder实现以下查询?

How can I implement the following query with Query Builder?

SELECT * 
FROM t 
WHERE t.status = 1
    OR EXISTS(SELECT * 
              FROM r 
              WHERE r.t_id = t.id 
                  AND r.status = 1
             )

没有存在检查的部分很容易,但是有一种实现 EXISTS的方法

The part without exist check is easy, but is there a way to implement the EXISTS?

推荐答案

您需要使用两个查询构建器:

You need to use two query builders:

$queryBuilder->expr()->exists($subQueryBuilder->getDql());

或直接使用DQL:

$queryBuilder->expr()->exists('SELECT * 
    FROM r 
    WHERE r.t_id = t.id 
    AND r.status = 1'
);

您将在文档中找到更多示例: http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html

You'll find more examples in the docs: http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html

这篇关于查询EXISTS for Doctrine Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆