- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道如何在 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/
我是一名优秀的程序员,十分优秀!