Java:INNER JOIN中的SELECT查询

时间:2019-02-09 19:13:12

标签: java sql postgresql jpa

下面的SQL查询在Postgres中可以很好地工作。它返回给定练习的所有最新培训。

SELECT th.id, th.date, th.exercise_id
FROM Training th
INNER JOIN (
  SELECT exercise_id, MAX(date) AS maxdate
  FROM Training
  GROUP BY exercise_id
  ) AS tm ON tm.exercise_id = th.exercise_id AND th.date = tm.maxdate

问题是Java JPA失败

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException unexpected token: (

在以下代码示例的INNER JOIN之后

String queryString = "SELECT th FROM TrainingHistory th INNER JOIN ( SELECT tm.exercise, MAX(date) as maxdate FROM TrainingHistory group by exercise ) AS tm on (tm.exercise = th.exercise AND th.date = tm.maxdate) WHERE th.accountId = 0";

我想念什么?

0 个答案:

没有答案