,我有以下表中的数据:返回单个最新记录每一个设备ID表
+-----+---------------------+---------------------+------------+--------+------+-----------------+
| 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 | [email protected] |
| 146 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T | 4 | 4 | [email protected] |
| 147 | 2017-03-08 16:54:00 | 2017-03-08 16:55:00 | T | 5 | 4 | [email protected] |
| 148 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T | 3 | 4 | [email protected] |
| 149 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T | 4 | 4 | [email protected] |
| 150 | 2017-03-08 16:55:00 | 2017-03-08 16:56:00 | T | 5 | 4 | [email protected] |
| 151 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T | 3 | 4 | [email protected] |
| 152 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T | 4 | 4 | [email protected] |
| 153 | 2017-03-08 18:28:00 | 2017-03-08 18:29:00 | T | 5 | 4 | [email protected] |
| 154 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F | 3 | 4 | [email protected] |
| 155 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F | 4 | 4 | [email protected] |
| 156 | 2017-03-08 18:32:00 | 2017-03-08 18:33:00 | F | 5 | 4 | [email protected] |
| 157 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F | 3 | 4 | [email protected] |
| 158 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F | 4 | 4 | [email protected] |
| 159 | 2017-03-08 18:58:00 | 2017-03-08 18:58:00 | F | 5 | 4 | [email protected] |
| 160 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 3 | 4 | [email protected] |
| 161 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 4 | 4 | [email protected] |
| 162 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 5 | 4 | [email protected] |
+-----+---------------------+---------------------+------------+--------+------+-----------------+
我需要的是如下的结果:
+-----+---------------------+---------------------+------------+--------+------+-----------------+
| 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 | [email protected] |
| 161 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 4 | 4 | [email protected] |
| 162 | 2017-03-08 19:02:00 | 2017-03-08 19:03:00 | F | 5 | 4 | [email protected] |
+-----+---------------------+---------------------+------------+--------+------+-----------------+
它需要只返回表格中每个设备ID的最新一条记录,并且仅在event_done状态为'F'或换句话说为false时才返回。
记录还必须处于正在进行请求的当前分钟,因为数据库将每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);
任何帮助将不胜感激。
在此先感谢
“的记录也必须在当前分钟请求正在被提出“并且如果没有这样的记录没有记录被返回?为什么这不是一个简单的'date_timer_off和date_timer_on和event_Done ='F''之间的WHERE sysdate()?它假定你不能有定时器重叠的设备,但是基于似乎合理的问题。 – xQbert
在阅读您尝试过的内容之后...在第一个datetime_off ...中将您的OR更改为并将您的> =改为<= ... select * from timers_data where event_done ='F'and(date_timer_off <= now()AND date_timer_on> = now())ORDER BY id desc;' – xQbert
谢谢@xQbert,如果解决方案一直存在问题,也会尝试。 – Barend