0

我有两张桌子:

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 中需要这个。我遇到的最大问题是结合左连接和子选择。

这可能吗?

4

1 回答 1

1

使用 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。

有关使用 Doctrine 2 的 Native SQL 的更多信息可以在这里找到:http: //docs.doctrine-project.org/en/latest/reference/native-sql.html

于 2012-06-15T10:06:12.853 回答