开发者

Sort a query by a varchar field(which have numerical values) on numerical basis with Jooq

开发者 https://www.devze.com 2023-02-28 17:23 出处:网络
How can i sort a varchar field , i have taken ID as varchar and i want to sort it numerically. We can do this in SQL with using

How can i sort a varchar field , i have taken ID as varchar and i want to sort it numerically.

We can do this in SQL with using

sql query ... order by 0+id desc

What i have in Jooq is

SimpleSelectFinalStep<ScoreCardRecord> q = factory.selectFrom(ScoreCard.SCORE_CARD)
                    .order开发者_C百科By(OloScoreCard.SCORE_CARD_ID.descending())
                    .limit(0,1);


Have you tried casting your VARCHAR field into a numeric DataType? You could do it like this:

factory.selectFrom(ScoreCard.SCORE_CARD)
       .orderBy(OloScoreCard.SCORE_CARD_ID.cast(Integer.class).descending())
       .limit(0, 1);

That way you could omit using "plain SQL fields"

0

精彩评论

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