JPQL order by expression子句:ORDER BY(x = y)ASC

时间:2012-08-25 17:38:19

标签: jpql

我试图通过像

这样的等式表达式对查询结果进行排序
   ORDER BY (x = y) ASC

但我一直得到意外的令牌=。我甚至试过像

这样的东西
   ORDER BY COUNT(x = y) ASC

但也没有成功:expecting CLOSE, found '='

有没有办法用JPQL实现这个目标?感谢

1 个答案:

答案 0 :(得分:1)

不,它不会像JPQL那样工作。这是不可能的,因为

  1. x=y无法订购
  2. x=y不是SELECT子句的一部分。
  3. 在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中使用它。