【问题标题】:SQL order by created and due date expiredSQL order by created and due date expired
【发布时间】:2014-06-29 16:56:30
【问题描述】:

我正在处理一个项目,我们有一系列请求。我需要按创建日期显示请求。但是,我还需要先显示那些到期日期已过的人。这是 POJO 类:

 @Entity
 @XmlRootElement
 @NamedQueries({
     @NamedQuery(name="Question.findAll", query="SELECT a FROM Request a ORDER BY a.created DESC"),
 })
 public class Request {

    Date created;
    Date duedate;
    String name;

    public Request () {}

    public static boolean isOverdue(){
         if duedate.before(new Date())) return true; else return false;
    }

    // omitting getter setters
 }

如何编写 JPQL 语句以获取按创建日期排序的请求列表,但首先显示到期日期已过期的请求列表?

【问题讨论】:

    标签: sql sql-order-by jpql


    【解决方案1】:

    我不知道 JPQL,但我假设你可以执行普通的 sql,例如:

    SELECT a FROM Request a ORDER BY coalesce(a.duedate, a.created) DESC

    或

    SELECT a FROM Request a ORDER BY coalesce(case when a.duedate < now() then duedate end, a.created) DESC

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多