1

我想MyTable只插入最后一个结果。所以我放了LIMIT 1orLIMIT 0,1但我有两个结果。它抛出一个异常。我能怎么做?我org.springframework.data.repository.CrudRepository在 mysql 数据库上使用

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

1 回答 1

1

尝试在 where 子句中添加一个条件,选择满足条件的最大日期。

@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")  
于 2013-02-05T10:05:57.607 回答