如何通过在spring数据本机查询中传递userid列表或通过存储库来获取用户的最大datetimestamp

slsn1g29  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(397)

我有一个表transactions,其中所有事务的详细信息将存储如下,

  1. +----+-------------+---------------------+---------------------+
  2. | id | merchant_id | amount | requested_timestamp |
  3. +----+-------------+---------------------+---------------------+
  4. | 1 | 5 | 10 | 2020-06-02 01:47:47 |
  5. | 2 | 5 | 20 | 2020-06-02 05:00:14 |
  6. | 3 | 744900 | 30 | 2020-06-02 05:00:27 |
  7. | 4 | 154427 | 100 | 2020-06-02 05:01:03 |
  8. | 5 | 504968 | 15 | 2020-06-02 05:01:26 |
  9. | 6 | 75703 | 20 | 2020-06-02 05:01:31 |
  10. | 7 | 732228 | 50 | 2020-06-02 05:01:59 |
  11. | 8 | 506342 | 25 | 2020-06-02 05:02:17 |
  12. | 9 | 504968 | 40 | 2020-06-02 05:02:36 |
  13. | 10 | 732228 | 30 | 2020-06-02 05:02:50 |
  14. +----+-------------+---------------------+---------------------+

我想找到最大时间戳的每一个商人在一个命中像传递清单的商品的查询。我试过使用spring数据,如下所示

  1. List<Transactions> findTop1ByMerchantIdInOrderByIdAsc(List<Integer> merchantIds);

但它没有给max时间戳。
我试了如下

  1. @Query("select merchantId,max(requestedTimestamp) from Transactions where merchantId in ?1")
  2. List<Object[]> getLastTxnsOfAllMerchants(List<Integer> merchantIds);

这是个例外。
即使使用本机查询,我的问题也将解决如下

  1. @Query(nativeQuery=true,value="select merchant_id,max(requested_timestamp) from transactions where merchant_id in ?1")
  2. List<Object[]> getLastTxnsOfAllMerchants(List<Integer> merchantIds);

这也有例外
请帮帮我谢谢。

fbcarpbf

fbcarpbf1#

你可以像这样运行查询

  1. SELECT merchant_id, MAX(requested_timestamp) FROM transactions
  2. GROUP BY merchant_id;

或者如果你想要特定的商人

  1. SELECT merchant_id, MAX(requested_timestamp) FROM transactions
  2. WHERE merchant_id IN (1, 2, 3)
  3. GROUP BY merchant_id;

希望这有帮助:)

相关问题