我正在尝试选择对特定列具有最大值的记录,这就是我到目前为止所拥有的:
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 ......)