Here are 2 tables of my Symfony2 project :
+-----------+ +----------------------------+
| EVENT | | PHOTO |
+-----------+ +------+-----------+---------+
| id | | id | event_id | likes |
+-----------+ +------+-----------+---------+
| 1 | | 1 | 1 | 90 |
| 2 | | 2 | 1 | 50 |
+-----------+ | 3 | 2 | 20 |
| 4 | 2 | 10 |
+------+-----------+---------+
I would like to select the 2 events with its most liked photo, which would look like :
+------------+------------+---------+
| event_id | photo_id | likes |
+------------+------------+---------+
| 1 | 1 | 90 |
+------------+----------------------+
| 2 | 3 | 20 |
+------------+----------------------+
The SQL solution is explained here (SQL Select only rows with Max Value on a Column) and could be :
SELECT p.event_id, p.likes, p.id
FROM photo p
INNER JOIN(
SELECT event_id, max(likes) likes
FROM photo
GROUP BY event_id
) ss on p.event_id = ss.event_id and p.likes = ss.likes
What would be the DQL query for that ? I tried many things but always get errors.