2013-02-05 34 views
0

我想有一個結果MyTable只有最後一次插入。 所以我把LIMIT 1LIMIT 0,1,但我有兩個結果,而不是。 它引發異常。 我該怎麼辦? 我對MySQL的使用org.springframework.data.repository.CrudRepository DB限制1與CrudRepository

@Query("select t from MyTable t " + 
     " where " + 
     " t.date <= :date " + 
     " order by t.date desc " + 
     " LIMIT 1 ") 
MyTable findOnlyTheLast(
     @Param("date") String date); 

回答

1

嘗試添加有條件的地方在於選擇符合條件的日期的最大條款。

@Query("select t from MyTable t " + 
     " where " + 
     " t.date <= :date " + 
     " and t.date = (select max(tt.date) from MyTable tt where tt.date <= :date)" + 
     " order by t.date desc") 
+0

你的建議很好! – Shinigami