相当于 Querydsl 中的"left"和"right" SQL 字符串操作函数?



我有一个类似于这样的SQL语句:

select left(right(my_string_column, 10), 5)
from my_table;

工作正常,它首先取my_string_column最右边的 10 个字符,然后从中给我这十个字符的前五个字符。我试图在 Querydsl 中找到与此类似的等效项,如下所示:

myTable.myStringColumn.right(10).left(5)

但是没有"左"和"右"的方法。我将如何在 Querydsl 中编写它? 谢谢!

我的建议是使用子字符串函数。 这是我在本地使用 querydsl v3.2.3 的示例:

QMyTable g = new QMyTable("g");
query = new SQLQuery(connection , dialect);
List<Tuple> l = query.from(g).list(g.mystringfield,
        g.mystringfield.substring(g.mystringfield.length().subtract(10),
        g.mystringfield.length() ).substring(0, 5).as("mystringfield5"));
for (Tuple t : l) {
    System.out.println(t.get(g.mystringfield) + " =====> " + t.get(1, String.class));
}

生成的查询如下所示:

select g.mystringfield,
(substr(substr(g.mystringfield,(length(g.mystringfield) - ?)+1,length(g.mystringfield)-(length(g.mystringfield) - ?)),1,5)) as mystringfield5
from MyTable g

左/右支持尚未在Querydsl中实现。您可以使用子字符串(0,大小)轻松模拟左边,但右边更棘手,我建议为它创建一个票证。

最新更新