0

这是我想要的查询:

SELECT * FROM (SELECT * FROM counter ORDER BY id DESC) AS x GROUP BY campaign_id

但我找不到在我的 Symfony 应用程序中表达这一点的方法。在 Doctrine 中使用这样的子查询的正确方法是什么?

这个:

$query = $em->createQuery('SELECT c
        FROM InstacountInstacountBundle:Counter c
        GROUP BY c.campaign');  
    $counts = $query->getResult();

工作正常,但我只想要我组中的最新记录。谢谢你的帮助!

4

1 回答 1

0
SELECT x.* 
  FROM counter x
  JOIN 
     ( SELECT campaign_id, MAX(id) max_id FROM counter GROUP BY campaign_id ) y
    ON y.campaign_id = x.campaign_id
   AND y.max_id = x.id;
于 2013-09-19T12:47:14.467 回答