Grails - 按查询中的两个字段排序 [英] Grails - Sort by two fields in a query

查看:135
本文介绍了Grails - 按查询中的两个字段排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Log {

整型entityId
整数tableId
日期logDt
}

我想按特定的tableId选择所有记录,并按entityId logDt desc对它们进行排序。 b

  Log.findAllByTableId(tableID,[sort:'entityId',order: 'desc'])

但是当我尝试按两个字段排序时:



Log.findAllByTableId(tableID,[sort:'entityId,logDt',order:'desc'])



我得到一个错误,表中没有这样的字段'entityId,logDt'在这张表上。



正确的语法是什么?



谢谢。

解决方案

使用动态查找器,您可以按一个属性进行排序。



如果您想按多个属性排序,您可以使用条件或HQL查询。



是一个使用标准的例子:

pre code def log = Log.createCriteria()。list {
eq('tableId ',tableID)
order('entityId','desc')
order('logDt','desc')
}
pre>

I Have Such a domain class in my project:

class Log  {

Integer entityId
Integer tableId
Date logDt
}

I would like to select all the records by a certain tableId, and sort them by entityId and logDt desc. Sorting by one filed works fine:

Log.findAllByTableId(tableID, [sort: 'entityId', order: 'desc'])

but when I try to sort by both fields:

Log.findAllByTableId(tableID, [sort: 'entityId,logDt', order: 'desc'])

I get an error that there is no such field 'entityId,logDt' at this table.

What is the right syntax to do so?

Thanks.

解决方案

Using the dynamic finders, you just can sort by one property.

If you would like to sort by multiple properties you could use a criteria or a HQL query.

Here is an example using a criteria:

def logs = Log.createCriteria().list {
    eq('tableId', tableID)
    order('entityId', 'desc')
    order('logDt', 'desc')
}

这篇关于Grails - 按查询中的两个字段排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆