有没有一种方法可以获得带有投影(仅限选定字段)的不同订单对象列表(基于 customerName
)?
假设只有 ID 不同,我想获取具有唯一 customerName
的订单。是否可以使用投影或任何其他方式?
我的代码是:
def criteria = Order.createCriteria()
def orders = criteria.list() {
and {
eq("showAddress", true)
like("customerName", "%abcdPqrs%")
}
projections {
distinct("customerName")
property("deliveryAddress")
property("billingAddress")
property("")
}
}
return orders
上面的代码从 Order 中获取了重复的 (customerName
) 记录,我该如何解决这个问题?
最佳答案
如果您将看到由 GORM 生成的 SQL 查询,您会发现 distinct 将应用于完整的行而不是 customerName。您可以通过放置
来启用日志logSql = true
在 datasource.groovy 中。
你可以试试这个
def criteria = Order.createCriteria()
def orders = criteria.list() {
and {
eq("showAddress", true)
like("customerName", "%abcdPqrs%")
}
projections {
groupProperty("customerName")
property("deliveryAddress")
property("billingAddress")
property("")
}
}
关于grails - 如何在 grails 中获取带有投影的不同记录列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43095793/