繁体   English   中英

如何使用 spring 数据 jpa 返回该日期的日期和记录数?

[英]How can I return date and count of records for that date using spring data jpa?

我想按日期返回事件计数。下面是我的代码

@Query(value = "select DATE(e.startTime) as day,count(distinct e.id) as eventCount from events e " +
            "where e.clientId=:clientId and e.startTime>=:firstDay and e.startTime<=:lastDay group by DATE(e.startTime)",nativeQuery = true)
    Map<Integer, Integer> getClientEventsCountByDay(@Param(value = "clientId") Integer clientId,
                                                    @Param(value = "firstDay") String firstDay,
                                                    @Param(value = "lastDay") String lastDay);

输入是clientId=2510,firstDay=2019-01-01,lastDay=2019-01-31

此查询抛出以下错误

引起:org.springframework.dao.IncorrectResultSizeDataAccessException: result 返回多个元素; 嵌套异常是 javax.persistence.NonUniqueResultException: result 返回多个元素

但是,当直接在 db 上运行时,此查询工作正常。 在此处输入图片说明

试试改成这个

List<Map<Integer, Integer>> getClientEventsCountByDay(@Param(value = "clientId") Integer clientId,
                                                @Param(value = "firstDay") String firstDay,
                                                @Param(value = "lastDay") String lastDay);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM