简体   繁体   中英

Translating sql query into Doctrine/dql

This is the query I want:

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

but I can´t find a way to express this in my Symfony application. What is the correct way to use subqueries like this one in Doctrine?

This one:

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

is working fine, but I want only the latest records in my group. Thanks for any help!

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM