我试过正常的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 10 和 11
但是我试图在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
答案 0 :(得分:0)
首先让我简化该查询,以便得到相同的结果:
SELECT id FROM `activity_shares`
GROUP BY `id`
HAVING COUNT(`activity_id`) > 1
您可以使用简化的SQL构建查询:
$results =$this->getEntityManager()->createQueryBuilder('c')
->add('select','c.id')
->add('from','MyBundleDataBundle:ActivityShare c')
->groupBy('c.id')
->having('count(c.activity)>1');
->getResult();
在这里你必须使用连接,但计数可能很棘手
解决方案1
像使用权限一样使用关联表(因为我看到你只需要id) 假设表名是activityshare_activity
它将有两个字段activity_id和activityshare_id,如果您找到一种方法将新列ID添加到该表并使其自动增量+主要,其余的很容易:
新实体名为 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();
添加新标识列以使其与教义兼容的步骤(您需要执行一次):
添加列(INT,NOT NULL)不要自动增量
ALTER TABLE tableName ADD id INT NOT NULL
使用类似于
将列修改为自动增量
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%确定有+计数的输出,但它应该说得很好:)
答案 1 :(得分: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