MySQL从datetime字段的最近5天选择*

时间:2013-10-24 11:03:04

标签: mysql sql datetime

我有一张看起来像这样的表

id  datetime  tick_info(string)
0  16-10-2013 3:33:01  "33300"
1  17-10-2013 5:04:01  "003023"
2  17-10-2013 6:12:04  "3244"
3  19-10-2013 5:32:12  "3333332"
4  20-10-2013 8:14:44  "33321"
5  20-10-2013 9:12:11  "5821"
6  22-10-2013 10:32:11 "33111"

所以数据可以跨越20天,我想选择最近5天的所有行。请注意,过去5天意味着:表格中最近的5个不同日期。

Ex:17-19-20-22-23如果21和18没有tick_info。

tick_info的数量是可变的,因此20可能有100个tick_info,23个可能有5个。

我正在使用mysql。

更新应该提到我正在使用mysql 5.5.32

3 个答案:

答案 0 :(得分:1)

试试这个::

Select 
* 
from 
'myTable' 
where 'datetime' >=
      (
           select  
          `datetime` 
          from myTable 
          group by DATE(`datetime`) 
          order by `datetime` desc 4,1
        )

答案 1 :(得分:1)

SELECT * FROM yourTable WHERE tick_info>=DATE_SUB(tick_info, INTERVAL 5 DAY) group by tick_info order by tick_info DESC

答案 2 :(得分:0)

请检查我的查询是百分之百的工作

我测试了它

Template.homePage.helpers({
  'notifications'() {
    let userNotCol = userNotCol.find({ userID: Meteor.userId(), read: false });
    userNotCol.observeChanges({
      added: function(id, fields){
        if(!fields.read){
          console.log('added'); // This enters in infinite loop 
        }
      }
    });
    return userNotCol;
  }
});
相关问题