【问题标题】:How to do query based on "date" column in sqlite3 using sequelize?如何使用 sequelize 基于 sqlite3 中的“日期”列进行查询?
【发布时间】:2014-06-17 16:27:06
【问题描述】:

我正在使用带有 sqlite3 的 Sequelize,但在查询基于“日期”类型列的数据时遇到问题。

这是我的模型定义:

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

现在我想在这个函数中获取所有“时间”晚于给定时间的记录:

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);
}

我在 Sequelize 的网站上找不到任何关于基于“日期”类型的查询语句的信息。

有什么想法吗?

【问题讨论】:

  • 你试过where: {time: {gt: new Date(...)}}吗?

标签: node.js sqlite sequelize.js


【解决方案1】:

你可以像这样使用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')
}

【讨论】:

    【解决方案2】:

    就像 Soumya 说的那样,您可以使用 $gt$lt$gte$lte 与日期:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2016-08-10
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多