简体   繁体   English

Django MySQL查询-选择DateTime 60分钟,然后选择上一行吗?

[英]Django MySQL Query - Select DateTime 60 minutes Larger Then Previous Row?

I have a model that has datetime filed called "start_time". 我有一个已归档datetime的模型,称为“ start_time”。

Lets say I do basic select query: 可以说我做了基本的选择查询:

results = MyModel.objects.filter(start_time=somedate).order_by('start_time')

I will get something like: 我会得到类似:

2015-07-10 17:15:00
2015-07-10 19:15:00
2015-07-10 19:45:00
2015-07-10 21:15:00
2015-07-10 21:45:00

My goal is to exclude all rows that are not at least 60 minutes larger then the previous row so I would get this result: 我的目标是exclude不比上一行大至少60分钟的所有行,以便得到以下结果:

2015-07-10 17:15:00
2015-07-10 19:15:00
2015-07-10 21:15:00

Any suggestions? 有什么建议么?

The corresponding sql query might look like this: 相应的sql查询可能如下所示:

SELECT * FROM Table
WHERE 
  start_time > somedate
GROUP BY 
  DATE(start_time), 
  HOUR(start_time)

I'm not sure if django ORM supports this kind of GROUP BY . 我不确定django ORM支持这种GROUP BY If not you may try raw sql. 如果没有,您可以尝试使用原始sql。

Edit: The 60 minute or 1 hour is covered up by grouping it by date-hour.It then takes the first element of each group. 编辑:将60分钟或1个小时按日期-小时分组进行掩盖,然后采用每个组的第一个元素。 If you are talking about 90 minutes or anything that doesn't fit into datetime field then I'm afraid , this approach will not work. 如果您正在谈论90分钟或日期时间字段中不适合的任何内容,那么恐怕这种方法将行不通。 The filtering should be done manually. 过滤应手动完成。

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

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