【问题标题】:Where-Condition as IN(Subquery) with Doctrine2 in Symfony2.3.1 doesnt work在 Symfony2.3.​​1 中使用 Doctrine2 作为 IN(子查询)的 Where-Condition 不起作用
【发布时间】:2013-08-26 15:38:34
【问题描述】:

---- 使用 Symfony2.3.​​1 和 Doctrine2 完成 ----

抱歉,我希望我没有太笨,无法找到适合我的问题的解决方案。我尝试构建一个查询数小时。

SELECT * FROM product
    WHERE product_id in
    (
       SELECT product_id from (
          SELECT count(*) as result_amount, product_id FROM product_x_attribut
          JOIN productattribut on productattribut_id = productattribut.id
          WHERE (
            productkey = "price" and
            stringType = "premium"
          ) or (
            productkey = "size" and 
            stringType = "S"
          )
          GROUP BY product_id
          HAVING result_amount = 2
       ) as temp
    )
GROUP BY product_id
ORDER BY p0_.name ASC

这是在 phpmyAdmin 中运行良好的 SQL。

可以这样看

Select * from abc where abc.x in ( Select * from ( select * from ) as abcd )

所以有一个核心查询,我称之为 subSubQuery,围绕核心的第二个查询将称为 subQuery,而外部查询只是外部查询,没有子查询。 我可以用 Doctrine2 构建 subSubQuery。

但我不能像这样构建子查询

Select product_id from ( $subSubQuery->getQuery()->getDQL() )

我想做这样的子查询

$subQuery = $repositoryProduct->createQueryBuilder('product');
$subQuery->add('select', 'product_id');
$subQuery->add('from',$subSubQuery->getDQL() );
// However to set an alias is a miracle for me, this didnt work off course
$subQuery->add('as','tmp' );

这是子查询。 我也无法构建外部查询

Select * from abc where abc.x in ( $subQuery->getQuery()->getDQL() )

我想这样做

$query->where(
  $query->expr()->in('product.id', $subQuery->getDQL() )
);

但我尝试像这样使用 Doctrine2 构建它:

我很沮丧,我尝试了 ->getSQL()、->getDQL(),我尝试了尽可能多的方法来解决这个问题,我尝试了尽可能多的关键字在谷歌,因为我的手指能够写...我希望有人可以帮助我找到解决方案...

非常感谢每一个有用的建议。

【问题讨论】:

    标签: symfony doctrine-orm where subquery


    【解决方案1】:

    我知道这样的陈述很有效:

    $qbGames->andWhere($qbGames->expr()->in('game.id',$qbGameId->getDQL()));
    

    您的问题有点难以理解。考虑使用 pastebin 来显示当前存在的所有映射。然后可能会提出一个简单的查询?

    我的 $qbGameId 查询是使用以下内容构建的:

       $qbGameId = $em->createQueryBuilder();
    
       $qbGameId->addSelect('distinct gameGameId.id');
    
       $qbGameId->from('ZaysoCoreBundle:Event','gameGameId');
    
       // Assorted joins and where conditions
    

    【讨论】:

    • 我是这样做的,但我得到一个错误“[语义错误] line 0, col 20 near 'SELECT count(productxattribut.id),': Error: Class 'SELECT' is not defined . ” 因为 $subQuery->getDQL() 返回“SELECT count(p0_.id) AS sclr0, p1_.id AS id1 FROM produkt_x_attribut p0_ INNER JOIN produktattribut p2_ ON p0_.produktattribut_id = p2_.id INNER JOIN produkt p1_ ON p0_。 produkt_id = p1_.id WHERE (p2_.schluessel = ? AND p0_.stringType = ?) OR (p2_.schluessel = ? AND p0_.stringType = ?) GROUP BY p0_.produkt_id HAVING count(p0_.id) = ?"跨度>
    • 是的。问题是我不知道您实际尝试了什么查询。你的问题太多了。尝试更新您的问题并说出类似的话:我尝试了这个确切的查询,但收到了这个错误消息。我更新了答案,您的子查询中可能没有使用 distinct?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2013-09-18
    • 2014-08-03
    • 1970-01-01
    相关资源
    最近更新 更多