SQL子查询IN SELECT [Symfony3]

时间:2017-09-25 15:47:54

标签: mysql sql symfony doctrine createquery

我的表操作包含以下字段:

  • idAction
  • 讨论终结
  • 日期

我想循环浏览并显示所有idAction在我的数据库中的列表+ idActionCloture = 0分组相同的日期(两者都有)。< / p>

我试过以下方法。但它不起作用。任何人都可以帮助我吗?

$query = $this->getEntityManager()->createQuery(

'SELECT COUNT(a.idAction) AS nmbreAction , week(a.dateOuverture) AS week,( SELECT COUNT(c.idAction) , week(c.dateOuverture) FROM ActionActionBundle:Action c 
WHERE c.cloture = 0 ) AS nmbreRetard FROM ActionActionBundle:Action a 
GROUP BY week');

1 个答案:

答案 0 :(得分:0)

嗯,你的问题缺乏很多信息。这是你需要的吗?

SELECT COUNT(a.idAction) AS nmbreAction ,
       week(a.dateOuverture) AS week,
       (SELECT COUNT(c.idAction) 
        FROM ActionActionBundle:Action c
        WHERE c.cloture = 0 
          and week(c.dateOuverture) = week(a.dateOuverture)) AS nmbreRetard 
FROM ActionActionBundle:Action a
GROUP BY week(a.dateOuverture)

您无法在选择列表中的子查询\相关查询中选择多于一列,这可能会在错误消息中显示给您。

编辑:更好的是:

SELECT COUNT(a.idAction) AS nmbreAction ,
       week(a.dateOuverture) AS week,
       COUNT(CASE WHEN a.cloture = 0 THEN 1 END) as nmbreRetard
FROM ActionActionBundle:Action a
GROUP BY week(a.dateOuverture)