我有两张表:
author (id, first_name, last_name)
books (id, title, rate, author_id)我需要得到评价最高的书的作者(每个作者一个)。
在sql中:
SELECT a.*, highest_rated_book.*
FROM authors a
LEFT JOIN (SELECT * FROM books b ORDER BY b.rate DESC) AS highest_rated_book
ON a.id = highest_rated_book.author_id
GROUP BY highest_rated_book.author_id
ORDER BY a.id;但是在Doctrine 2中我需要这样做,我遇到的最大的问题是组合使用左连接和子选择。
这个是可能的吗?
发布于 2012-06-15 18:06:13
使用DQL,您似乎只能连接关联的实体(参见http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/dql-doctrine-query-language.html#from-join-and-index-by)。
您可以做的是使用原生查询,这允许您使用原始sql。
有关Doctrin2的原生SQL的更多信息可以在这里找到:http://docs.doctrine-project.org/en/latest/reference/native-sql.html
https://stackoverflow.com/questions/11041474
复制相似问题