开发者

Grails/Hibernate max for string type

开发者 https://www.devze.com 2022-12-30 07:09 出处:网络
In my table I have a serial number field, which is represneted by string.. It has a prefix and some numbers follow. Eg: ABC1234, ABC2345 etc. How to retrieve the largest value (max equivale开发者_运维

In my table I have a serial number field, which is represneted by string.. It has a prefix and some numbers follow. Eg: ABC1234, ABC2345 etc. How to retrieve the largest value (max equivale开发者_运维技巧nt of int type) from this column. In my case it would be ABC2345. I probably could retrieve all the data,, sort it and get the same, but that would be slow.

thanks in advance..


You need to utilize GORM criteria, projections in particular:

def c = MyEntity.createCriteria()

def maxNumber = c.get {
    projections {
        max("serialNumber")
    }
}

This assumes that your entity is named MyEntity and field is named serialNumber.

0

精彩评论

暂无评论...
验证码 换一张
取 消