【问题标题】:Get data of last record with join in jpa在jpa中加入最后一条记录的数据
【发布时间】:2017-12-22 12:52:52
【问题描述】:

我使用带有休眠实现和 postgres 的 spring data jpa 我有两张桌子

台式机:ID、名称
表记录:id、machine_id、temp1、time_stamp

我搜索显示每台机器的最后一条记录(id、machine_id、name、temp1)

这个 sql 查询似乎可以完成这项工作

select r.*, d.*
from machine d
join record r on (d.id=r.machine_id)
left outer join record r2 on (d.id=tr2.machine_id and
(r.time_stamp<r2.time_stamp or r.time_stamp=r2.time_stamp and  r.id<r2.id)
)
where r2.id is null

但我在 jpa 中搜索这样做

【问题讨论】:

  • 请明确你想要什么... JPQL 中的这个 sql?
  • 它清楚地写了查询是在 sql 中......我搜索在 jpql 中写它..

标签: postgresql jpa spring-data-jpa


【解决方案1】:

我猜您在 Machine 和 Record 实体之间存在双向 OneToOne 关系。

select machine
from Machine machine
inner join machine.record record
left join machine.record record2 

where r2.id IS NULL

and (machine = record2.machine and record.time_stamp < record2.time_stamp or record.time_stamp = recrod2.time_stamp and record.id < record2.id)

【讨论】:

    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2011-11-06
    • 2020-09-03
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多