我有以下表格数据:
+-----+---------------------+---------------------+------------+--------+------+-----------------+
| id | date_timer_off | date_timer_on | event_done | device | user | admin_email |
+-----+---------------------+---------------------+------------+--------+------+-----------------+
| 145 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T | 3 | 4 | admin@admin.com |
| 146 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T | 4 | 4 | admin@admin.com |
| 147 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T | 5 | 4 | admin@admin.com |
| 148 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T | 3 | 4 | admin@admin.com |
| 149 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T | 4 | 4 | admin@admin.com |
| 150 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T | 5 | 4 | admin@admin.com |
| 151 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T | 3 | 4 | admin@admin.com |
| 152 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T | 4 | 4 | admin@admin.com |
| 153 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T | 5 | 4 | admin@admin.com |
| 154 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F | 3 | 4 | admin@admin.com |
| 155 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F | 4 | 4 | admin@admin.com |
| 156 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F | 5 | 4 | admin@admin.com |
| 157 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F | 3 | 4 | admin@admin.com |
| 158 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F | 4 | 4 | admin@admin.com |
| 159 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F | 5 | 4 | admin@admin.com |
| 160 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 3 | 4 | admin@admin.com |
| 161 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 4 | 4 | admin@admin.com |
| 162 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 5 | 4 | admin@admin.com |
+-----+---------------------+---------------------+------------+--------+------+-----------------+
我需要的结果如下:
+-----+---------------------+---------------------+------------+--------+------+-----------------+
| id | date_timer_off | date_timer_on | event_done | device | user | admin_email |
+-----+---------------------+---------------------+------------+--------+------+-----------------+
| 160 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 3 | 4 | admin@admin.com |
| 161 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 4 | 4 | admin@admin.com |
| 162 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 5 | 4 | admin@admin.com |
+-----+---------------------+---------------------+------------+--------+------+-----------------+
只有在event_done状态为“F”或其他字母为false时,才需要在表格中仅返回每个设备ID的最新一条记录。
记录也必须在发出请求的当前分钟内,因为将每20秒查询一次数据库以更新定时器列表。
例如,如果请求是在19:02:20进行的,则应检查以检查date_timer_off或date_timer_on字段是否在请求发出的时间内(19:02:20),从而返回上述内容结果集。
我尝试实施这篇文章SQL query for returning the latest record for each ID的答案,但没有成功。
这是我在生产环境中使用的当前查询,但它会导致下一个事件无法触发等问题。
select * from timers_data data where data.date_timer_off >= :today AND data.date_timer_off <= :tomorrow AND data.date_timer_on >= :today AND data.date_timer_on <= :tomorrow AND data.event_done = 'F' ORDER BY data.id DESC
我也试过这个:
select * from timers_data where event_done = 'F' and (date_timer_off >= now() or date_timer_on >= now()) ORDER BY id desc;
它在MySQL上运行时有效,但是当我使用Spring的JPA CrudRepository在我的Spring应用程序中运行它时似乎有问题,如下所示:
@Query(value = "select * from timers_data where event_done = 'f' and (date_timer_off >= now() or date_timer_on >= now()) ORDER BY id desc;", nativeQuery = true)
ArrayList<TimersData> findTimersForToday();
这是我目前在项目中使用的内容:
@Query(value = "select * from timers_data data where data.date_timer_off >= :today AND data.date_timer_off <= :tomorrow AND data.date_timer_on >= :today AND data.date_timer_on <= :tomorrow AND data.event_done = 'F' ORDER BY data.id DESC", nativeQuery = true)
ArrayList<TimersData> findTimersForToday(@Param("today") Date today, @Param("tomorrow") Date tomorrow);
任何帮助将不胜感激。
提前致谢
答案 0 :(得分:0)
您可以使用带有in子句的subselect,该子句基于与设备匹配的元组和max(date ..)
select * from timers_data data
where (device, date_timer_off) in (
select device, max(date_timer_off)
from timers_data data
where data.date_timer_off >= :today
AND data.date_timer_off <= :tomorrow
AND data.date_timer_on >= :today
AND data.date_timer_on <= :tomorrow
AND data.event_done = 'F'
group by device )
ORDER BY data.id