简体   繁体   English

如何使用sequelize基于sqlite3中的“日期”列进行查询?

[英]How to do query based on “date” column in sqlite3 using sequelize?

I'm using Sequelize with sqlite3 but having trouble querying data based on "date" type columns. 我在sqlite3中使用Sequelize,但是在基于“日期”类型列查询数据时遇到了麻烦。

Here is my model definition: 这是我的模型定义:

var sensorData = sequelize.define('Data', 
{
    time: Sequelize.DATE,
    value: Sequelize.FLOAT,
    type: Sequelize.STRING,
    source: Sequelize.STRING
},
{
    timestamps : false
});

Now I want to get all the records with 'time' later than a given time in this function: 现在,我想在此函数中获取比给定时间晚的所有带有“时间”的记录:

function selectData(start_date, num_of_records, callback)
{
    sensorData.findAll({
        limit: num_of_records, 
        where: [time, '???'], <====== How to specify the condition here?
        order: 'time'}).success(callback);
}

I can't find any thing on Sequelize's website about 'date' type based query statement. 我在Sequelize的网站上找不到有关基于“日期”类型的查询语句的任何信息。

Any idea? 任何的想法?

Just like Soumya says, you can use $gt , $lt , $gte or $lte with a date: 就像Soumya所说的那样,您可以将$gt$lt$gte$lte与日期一起使用:

model.findAll({
  where: {
    start_datetime: {
      $gte: new Date()
    }
  }
})

You can use sequelize.fn like this: 您可以像这样使用sequelize.fn

where:{timestamp:    gte:sequelize.fn('date_format', fromDate, '%Y-%m-%dT%H:%i:%s')
            ,lte:sequelize.fn('date_format', toDate, '%Y-%m-%dT%H:%i:%s')
}

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

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