gpt4 book ai didi

postgresql - 如何在 Doctrine 2 中使用 "DISTINCT ON (field)"?

转载 作者:行者123 更新时间:2023-11-29 11:15:03 25 4
gpt4 key购买 nike

我知道如何在 Doctrine 2 中使用“DISTINCT”,但我确实需要使用“DISTINCT ON (field)”,但我不知道如何使用 QueryBuilder 执行此操作。

我的 SQL 查询如下所示:

SELECT DISTINCT ON (currency) currency, amount FROM payments ORDER BY currency

这个查询工作得很好,但我不能将它与 QueryBuilder 一起使用。也许我可以用其他方式编写此查询?

最佳答案

我建议 PostgreSQL 支持的 SELECT DISTINCT ON (..) 构造在 Doctrine 的核心对象关系模型 (ORM) 之外。或者,换句话说,因为 SELECT DISTINCT ON (..) 在 SQL 实现中很少见,Doctrine 还没有为它编码。

不管它不工作的实际逻辑如何,我建议你试试 Doctrine 的“Native SQL”。您需要将查询结果映射到 ORM。

With NativeQuery you can execute native SELECT SQL statements and map the results to Doctrine entities or any other result format supported by Doctrine.

In order to make this mapping possible, you need to describe to Doctrine what columns in the result map to which entity property. This description is represented by a ResultSetMapping object.

With this feature you can map arbitrary SQL code to objects, such as highly vendor-optimized SQL or stored-procedures.

SELECT DISTINCT ON (..) 我认为属于vendor-optimized SQL,因此使用 NativeQuery 应该允许您访问它。

关于postgresql - 如何在 Doctrine 2 中使用 "DISTINCT ON (field)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11791748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com