有没有一种方法可以获得带有投影(仅限选定字段)的不同 Order 对象(基于 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/