开发者

Grails/Hibernate max for string type

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜