1

我在 sql 中有这个查询:

SELECT 
SUBSTR(report_number, STRPOS(report_number, '/SW/')),
SUBSTR(report_number,STRPOS(report_number, ''), STRPOS(report_number, '/')-1) 
FROM 
do_study
ORDER BY 
SUBSTR(report_number, STRPOS(report_number, '/SW/')),
CAST(SUBSTR(report_number,STRPOS(report_number, ''), STRPOS(report_number, '/')-1)as decimal(6,0)) 

结果:

"/SW/2015";"2"
"/SW/2017";"1"
"/SW/2017";"3"
"/SW/2017";"10"
"/SW/2017";"11"

它做我想做的事。现在我需要在条件查询中进行排序。我写这个:

List<Order> list = new ArrayList<>();
list.add(cb.asc(
        cb.substring(queryRoot.get(DoStudy_.reportNumber),
                cb.locate(queryRoot.get(DoStudy_.reportNumber), "/SW/")
        )
));
list.add(cb.asc(
                cb.substring(queryRoot.get(DoStudy_.reportNumber),
                        cb.locate(queryRoot.get(DoStudy_.reportNumber), ""),
                        cb.diff(
                               cb.locate(queryRoot.get(DoStudy_.reportNumber), "/"), 1
                        )
                )
 ));

return list;

结果:

"/SW/2015";"2"
"/SW/2017";"1"
"/SW/2017";"10"
"/SW/2017";"11"
"/SW/2017";"3"

我认为它将第二列解释为字符串。我应该怎么做才能得到预期的结果?我试试这个:

list.add(cb.asc(
                cb.toInteger((Expression<Integer>)(Expression<?>)cb.substring(queryRoot.get(DoStudy_.reportNumber),
                        cb.locate(queryRoot.get(DoStudy_.reportNumber), ""),
                        cb.diff(
                               cb.locate(queryRoot.get(DoStudy_.reportNumber), "/"), 1
                        )
                )
       )
 ));

但这并没有改变任何东西。感谢您的帮助。

4

0 回答 0