我正在尝试选择具有特定列的最大值的记录,这是我到目前为止所拥有的:
select o from Order o
where o.orderNumber = :orderNumber
and o.version =
(select max(o.version) from Order o where o.orderNumber = :orderNumber)
这似乎是在生成SQL:
SELECT *
FROM Order
WHERE ordernumber = :orderNumber
AND orderversion = (SELECT Max(orderversion)
FROM order
WHERE ordernumber = :orderNumber);
我认为以下SQL会更有效:
SELECT *
FROM order ord
INNER JOIN (SELECT ordernumber,
Max (version) AS version
FROM order
WHERE ordernumber = :ordernumber
GROUP BY ordernumber) mx
ON ord.ordernumber = mx.ordernumber
AND ord.version = mx.version;
无论如何我可以在JPQL中表达这个吗?
(大多数在线答案与选择特定字段上的最大值记录相关的问题似乎表明我提出的上述JPQL ......)