这是我的 Symfony2 项目的 2 个表:
+-----------+ +----------------------------+
| EVENT | | PHOTO |
+-----------+ +------+-----------+---------+
| id | | id | event_id | likes |
+-----------+ +------+-----------+---------+
| 1 | | 1 | 1 | 90 |
| 2 | | 2 | 1 | 50 |
+-----------+ | 3 | 2 | 20 |
| 4 | 2 | 10 |
+------+-----------+---------+
我想选择最喜欢的照片的 2 个事件,看起来像:
+------------+------------+---------+
| event_id | photo_id | likes |
+------------+------------+---------+
| 1 | 1 | 90 |
+------------+----------------------+
| 2 | 3 | 20 |
+------------+----------------------+
此处解释了 SQL 解决方案(SQL Select only rows with Max Value on a Column)并且可能是:
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
什么是 DQL 查询?我尝试了很多东西,但总是出错。