Vapor 3/Fluent for Postgres:如何进行 SELECT DISTINCT 查询?
Vapor 3/Fluent for Postgres: How do I make a SELECT DISTINCT query?
我想查询一个模型,以便 Fluent 生成 SQL 如下所示:
SELECT DISTINCT ON(<my columns>) * FROM my_table...
我该怎么做?
感叹
因此,最终不得不使用原始查询。您可以使用以下方法执行相同操作:
let distinctModels = req.withPooledConnection(to: .psql) { (conn) -> Future<[MyModel]> in
conn.raw("SELECT DISTINCT ON(<my columns>) * FROM <my_table> INNER JOIN <another_table> ON <some_condition> WHERE <conditions>")
.all(decoding: MyModel.self)
}
其中 req
的类型为 Request
。函数 withPooledConnection
将 return 你模型的未来——只要确保你解码它们!
我想查询一个模型,以便 Fluent 生成 SQL 如下所示:
SELECT DISTINCT ON(<my columns>) * FROM my_table...
我该怎么做?
感叹
因此,最终不得不使用原始查询。您可以使用以下方法执行相同操作:
let distinctModels = req.withPooledConnection(to: .psql) { (conn) -> Future<[MyModel]> in
conn.raw("SELECT DISTINCT ON(<my columns>) * FROM <my_table> INNER JOIN <another_table> ON <some_condition> WHERE <conditions>")
.all(decoding: MyModel.self)
}
其中 req
的类型为 Request
。函数 withPooledConnection
将 return 你模型的未来——只要确保你解码它们!