1

我尝试使用普通的 sql 查询

SELECT activity_shares.id FROM `activity_shares` 
INNER JOIN (SELECT `activity_id` FROM `activity_shares`
GROUP BY `activity_id`
HAVING  COUNT(`activity_id`) > 1 ) dup ON activity_shares.activity_id = dup.activity_id

这给了我记录 id 说1011

但是我尝试在 中进行的相同查询Doctrine query builder

 $qb3=$this->getEntityManager()->createQueryBuilder('c')
           ->add('select','c.id')
           ->add('from','MyBundleDataBundle:ActivityShare c')
           ->innerJoin('c.activity', 'ca')
         //  ->andWhere('ca.id = c.activity')
           ->groupBy('ca.id')
          ->having('count(ca.id)>1');

编辑:

        $query3=$qb3->getQuery();
        $query3->getResult();

生成的 SQL 为:

SELECT a0_.id AS id0 FROM activity_shares a0_ 
INNER JOIN activities a1_ ON a0_.activity_id = a1_.id 
GROUP BY a1_.id HAVING count(a1_.id) > 1

只给出 1 条记录,即10条。我想同时获得。我不知道我哪里出错了。知道吗?

我的表结构是:

ActivityShare
    +-----+---------+-----+---
    | Id  |activity |Share| etc...
    +-----+---------+-----+----
    | 1   | 1       |1    |
    +-----+---------+-----+---
    | 2   | 1       | 2   |
    +-----+---------+-----+---

Activity是表的外键Activity。我想得到 ID 的 1 和 2

4

2 回答 2

0

感谢您的回答。我终于设法得到答案

我的教义查询是:

$subquery=$this->getEntityManager()->createQueryBuilder('as')
      ->add('select','a.id')
      ->add('from','MyBundleDataBundle:ActivityShare as')
      ->innerJoin('as.activity', 'a')
      ->groupBy('a.id')
      ->having('count(a.id)>1');



 $query=$this->getEntityManager()->createQueryBuilder('c')
      ->add('select','c.id')
      ->add('from','ChowzterDataBundle:ActivityShare c')
      ->innerJoin('c.activity', 'ca');

    $query->andWhere($query->expr()->in('ca.id', $subquery->getDql()))
      ;

    $result = $query->getQuery();
    print_r($result->getResult());

SQL 看起来像:

SELECT a0_.id AS id0 FROM activity_shares a0_ INNER JOIN activities a1_ ON a0_.activity_id = a1_.id WHERE a1_.id IN (SELECT a2_.id FROM activity_shares a3_ INNER JOIN activities a2_ ON a3_.activity_id = a2_.id GROUP BY a2_.id HAVING count(a2_.id) > 1
于 2013-03-05T09:56:05.070 回答
0

简化的 SQL

首先让我简化该查询,使其给出相同的结果:

SELECT id FROM `activity_shares` 
GROUP BY `id`
HAVING  COUNT(`activity_id`) > 1 

教义查询生成器

如果您像 sql 建议的那样将 activty 的 id 存储在表中:

您可以使用简化的 SQL 来构建查询:

$results =$this->getEntityManager()->createQueryBuilder('c')
           ->add('select','c.id')
           ->add('from','MyBundleDataBundle:ActivityShare c')
           ->groupBy('c.id')
           ->having('count(c.activity)>1');
           ->getResult();

如果您使用关联表(Doctrine 逻辑)

在这里你将不得不使用 join 但计数可能很棘手

解决方案 1

像实体一样使用关联表(我认为你只需要id)假设表名是activityshare_activity

它将有两个字段 activity_id 和 activityshare_id,如果您找到一种向该表添加新列 id 并使其成为 Autoincrement + Primary 的方法,其余的很容易:

名为ActivityShareActivity的新实体

$results =$this->getEntityManager()->createQueryBuilder('c')
           ->add('select','c.activityshare_id')
           ->add('from','MyBundleDataBundle:ActivityShareActivity c')
           ->groupBy('c.activityshare_id')
           ->having('count(c.activity_id)>1');
           ->getResult();

添加新标识列以使其与学说兼容的步骤(您需要这样做一次):

  1. 添加列 (INT , NOT NULL)不要放自动增量

    ALTER TABLE tableName ADD id INT NOT NULL

  2. 使用类似 for 的 php 循环填充列

  3. 将列修改为自增

    ALTER TABLE tableName MODIFY id INT NOT NULL AUTO_INCREMENT

解决方案2

更正您的查询

$result=$this->getEntityManager()->createQueryBuilder()
           ->select('c.id')
           ->from('MyBundleDataBundle:ActivityShare', 'c')
           ->innerJoin('c.activity', 'ca')
           ->groupBy('c.id') //note: it's c.id not ca.id
           ->having('count(ca.id)>1')
           ->getResult();

我最后发布了这个,因为我不能 100% 确定 have+ count 的输出,但它应该说得很好:)

于 2013-03-05T04:01:13.553 回答