在JPA查询中使用MySQL的LPAD字符串函数? [英] MySQL's LPAD String Function in JPA Query?

查看:332
本文介绍了在JPA查询中使用MySQL的LPAD字符串函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以知道如何将以下MySQL SQL转换为JPA查询吗?

May I know how to convert the following MySQL SQL to JPA query?

SELECT * FROM ORDER
WHERE LPAD(BATCH, 2, ' ') < LPAD('X', 2, ' ') ;

我不知道的是JPA中的LPAD()字符串函数.

What I couldn't figure out is the LPAD() string function in JPA.

你有什么主意吗?

推荐答案

我知道那是一篇很老的文章,但是当我的同事构建JPA查询如何做同样的事情时,我总是受到我的质疑. 几乎永远不会找到完美的拟合函数来执行您想要的操作.但是,使用某些逻辑,就可以在完整的JPA中获得相同的结果.

I know that is a very old post, but i am always be questioned by some colleagues when they are building JPA queries how to do the same thing. Almost never you will find a perfect fit function to do what do you want. But, using some logic you can get the same result, in full JPA.

LPAD的示例.

Expression<String> numberWithZeroLeft = builder.concat("0000", root.get("myNumber")); //Return something like 000012
Expression<Integer> expressionLength = builder.length(numberWithZeroLeft);
Expression<String> numberLpad = builder.substring(numberWithZeroLeft, e.builder.sum(expressionLength, -3), e.builder.literal(4)); //Return something like 0012

query.select(numberLpad);

请考虑以下内容:

I have a string X, and need a string formatted in 4 zeros.
So we will get 0000 + X => "0000X"
And now just get the 4 last characters using substring.

我希望能帮助别人.

干杯. =]

这篇关于在JPA查询中使用MySQL的LPAD字符串函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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