【问题标题】:Sequelize query to find all records that fall between a date range and time rangeSequelize 查询以查找日期范围和时间范围之间的所有记录
【发布时间】:2021-10-09 03:54:50
【问题描述】:

我有一个 Sequelize.DATE 类型的列 time_scheduled,其中存储了 events 的预定时间。

我正在尝试创建一个 Sequelize 查询,我可以在其中找到日期范围和时间范围之间的所有事件。相同的 SQL 查询如下。

SELECT * FROM events where time_scheduled between '2021/10/01' and '2021/10/10' and time_scheduled::timestamp::time between '12:00' and '15:00';

相同的示例输出

id time_scheduled
4d543320-4d23-46d2-8a54-fbb13a1251d0 2021-10-05 14:30:00+00
d6640e70-f873-436c-a59d-5a4c4fc655b7 2021-10-06 12:02:49.441+00
b11481b2-ffdd-413e-81af-f83df756bcc9 2021-10-06 13:55:36.62+00
53447517-f226-407f-94f4-63ddc8c17d9c 2021-10-07 13:59:48.123+00
f0344678-11eb-4422-9d23-43e8d5320f55 2021-10-07 14:14:13.647+00

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    您可以使用Op.between 以及Sequelize.whereSequelize.cast 来实现您想要的:

    const records = await Events.findAll({
      where: {
        [Op.and]: [{
          time_scheduled: {
            [Op.between]: ['2021/10/01', '2021/10/10']
          }
        }, 
        Sequelize.where(Sequelize.cast(Sequelize.col('time_scheduled'), 'time'), '>=', '12:00'),
        Sequelize.where(Sequelize.cast(Sequelize.col('time_scheduled'), 'time'), '<=', '15:00')
    ]
      }
    });
    
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2021-11-15
      • 2012-01-20
      • 1970-01-01
      相关资源
      最近更新 更多