hql 查询 合并查询根据不同条件的结果 并排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hql 查询 合并查询根据不同条件的结果 并排序相关的知识,希望对你有一定的参考价值。

 1 public List<?> getUntakedOrderList(Long userId, List statusList) {
 2         Session currentSession = this.getSessionFactory().getCurrentSession();
 3         String hql = "select orderInfo.orderStatus as orderStatus, orderInfo.orderTime as orderTime, areaRelationStart.areaCityName as startCityName, areaRelationStart.areaName as startAreaName, areaRelationEnd.areaCityName as endCityName, " +
 4                 "areaRelationEnd.areaName as endAreaName,userAddressStart.addressLocation as startAddress,userAddressEnd.addressLocation as endAddress,orderInfo.orderId as orderId," +
 5                 "userAddressEnd.addressName as addressName,orderInfo.orderCommonNo as orderCommonNo " +
 6                 "from OrderInfo as orderInfo left outer join OrderInfoAppend as orderInfoAppend on orderInfo.orderId=orderInfoAppend.appendOrderId " +
 7                 "left join UserAddress as userAddressStart on orderInfo.orderSendAddId=userAddressStart.addressId " +
 8                 "left join UserAddress as userAddressEnd on orderInfo.orderReceiveAddId=userAddressEnd.addressId " +
 9                 "left join AreaRelation as areaRelationStart on areaRelationStart.areaCode=userAddressStart.addressCity " +
10                 "left join AreaRelation as areaRelationEnd on areaRelationEnd.areaCode=userAddressEnd.addressCity" +
11                 " where (orderInfoAppend.appendOrderId = null and orderInfo.orderUserId=(:userId) and orderInfo.orderStatus in (:statusList) " +
12                 "and orderInfo.orderDeleted=false) or (orderInfo.orderUserId=(:userId) and orderInfo.orderStatus=(:orderStatus) and orderInfo.orderDeleted=false) order by orderInfo.orderTime desc ";
13         Query query = currentSession.createQuery(hql)
14                 .setParameter("userId", userId)
15                 .setParameterList("statusList", statusList)
16                 .setParameter("orderStatus", 290)
17                 .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
18         return query.list();
19     }

1.红色部分为第一部分的条件,蓝色部分为第二部分的条件

以上是关于hql 查询 合并查询根据不同条件的结果 并排序的主要内容,如果未能解决你的问题,请参考以下文章

合并两个查询并根据 Laravel 中的 created_at 对它们进行排序

根据 when 语句中的不同日期参数多次运行查询并合并结果,而不是多个 UNION ALL

如何编写 hql 查询或条件查询以根据时间戳获取最新记录

与 UNION 一起使用时,对每个 SQL Server 查询都不起作用的排序

hibernate的hql语句条件查询查不到结果?

MySql操作「数据查询条件20211223