Spring 需要帮助以JPQL编写查询,而不需要子查询吗

Spring 需要帮助以JPQL编写查询,而不需要子查询吗,spring,database,jpa,jpql,Spring,Database,Jpa,Jpql,我需要在单个数据库上执行一个简单的查询,但由于JPQL中缺少子查询(HAVING和EXIT子句除外),我不知道如何编写它 这张桌子看起来像这样 id | eventType | occurenceDate | comment | data | ... ----------------------------------------------------------- 1 | event-A | 2020-09-14 | ... 2 | event-B |

我需要在单个数据库上执行一个简单的查询,但由于JPQL中缺少子查询(HAVING和EXIT子句除外),我不知道如何编写它

这张桌子看起来像这样

id  |  eventType | occurenceDate   | comment | data | ...
-----------------------------------------------------------
1   |  event-A   | 2020-09-14      | ...
2   |  event-B   | 2020-09-09      | ...
3   |  event-A   | 2020-09-13      | ...
4   |  event-A   | 2020-09-10      | ...
5   |  event-B   | 2020-09-20      | ...
6   |  event-C   | 2020-09-11      | ...
我需要一个查询来按类型获取给定引用日期下发生的所有事件。 例如,如果参考日期是“2020-09-12”,我希望查询返回id=3和5的实体

获取eventType/occurenceDate元组列表(这是唯一的复合键)没有问题

但是没有子查询,我不知道如何获取完整的实体

有什么帮助吗?
谢谢

实际上,子查询是您通常在JPQL中处理此查询的方式:

SELECT t1
FROM table t1
WHERE t.occurenceDate > :referenceDate AND
      t.occurrenceDate = (SELECT MIN(t2.occurrenceDate)
                          FROM table t2
                          WHERE t2.eventType = t1.eventType);
在大多数数据库中,如果没有正式的子查询,就无法获取完整记录(SQL Server可能是一个例外,但即使在那里,您仍然必须使用本机非JPQL查询)。

确定。 只要稍微更改一下您的建议查询,它就可以工作了! 不知道有可能(或认为)在子查询中插入t1记录。 (是的,我的数据库知识很少^^)

多谢各位

SELECT t1
FROM table t1
WHERE t.occurenceDate > :referenceDate AND
      t.occurrenceDate = (SELECT MIN(t2.occurrenceDate)
                          FROM table t2
                          WHERE t2.eventType = t1.eventType);
SELECT t1
FROM table t1
WHERE t1.occurrenceDate = (
    SELECT MIN(t2.occurrenceDate)
    FROM table t2
    WHERE t2.eventType = t1.eventType 
    AND t2.occurenceDate > :referenceDate
);