我正在尝试通过相等表达式对查询结果进行排序,例如
ORDER BY (x = y) ASC
但我不断得到意想不到的令牌=
。我什至尝试过像
ORDER BY COUNT(x = y) ASC
但也没有成功:expecting CLOSE, found '='
有没有办法通过 JPQL 实现这一目标?谢谢
我正在尝试通过相等表达式对查询结果进行排序,例如
ORDER BY (x = y) ASC
但我不断得到意想不到的令牌=
。我什至尝试过像
ORDER BY COUNT(x = y) ASC
但也没有成功:expecting CLOSE, found '='
有没有办法通过 JPQL 实现这一目标?谢谢
不,它不适用于 JPQL。这是不可能的,因为
x=y
不可订购x=y
不是 SELECT 子句的一部分。在 JPA 2.0 规范中,这是用以下词语来说明的:
An orderby_item must be one of the following:
1. A state_field_path_expression that evaluates to an orderable state field
of an entity or embeddable class abstract schema type designated in the
SELECT clause by one of the following:
• a general_identification_variable
• a single_valued_object_path_expression
2. A state_field_path_expression that evaluates to the same state field of
the same entity or embeddable abstract schema type as a
state_field_path_expression in the SELECT clause
3. A result_variable that refers to an orderable item in the SELECT clause
for which the same result_variable has been specified. This may be the
result of an aggregate_expression, a scalar_expression, or a
state_field_path_expression in the SELECT clause.
您可以尝试在 SELECT 子句中使用 CASE 来构造数字属性并在 ORDER BY 中使用它。