根据CURRENT_DATE对js进行日期比较

时间:2018-10-11 12:08:55

标签: sequelize.js

在phpMyAdmin上执行此操作会给我一个记录。

SELECT * FROM `banners` WHERE region_id = 1 AND status= 'approved' and approved_date = CURRENT_DATE;

但是我尝试使用这样的序列化来实现相同的目的

const banners = await db.banners.findAll({
      where: {
        region_id: id,
        status: "approved",
        approved_date: new Date()
      }
    });

当我跟踪查询时

选择idpathexpire_datestatusfeenotesapproved_date,{{1} },  region_idrestaurant_id的{​​{1}},is_paidcreatedAtupdatedAt bannersbanners = 1 AND bannersregion_id ='approved'AND  bannersstatus ='2018-10-11 17:29:31';

我看到的问题是banners列数据类型是日期和比较approved_date 日期和时间。 sequlize中与CURRENT_DATE类似的关键字是什么

2 个答案:

答案 0 :(得分:2)

如果您想直接将字符串传递给WHERE条件,则可以使用sequelize.literal

const banners = await db.banners.findAll({
  where: {
    $and: [
      {
        region_id: id,
        status: "approved"
      }
    ],
    sequelize.where(
      sequelize.fn('DATE', sequelize.col('approved_date')),
      sequelize.literal('CURRENT_DATE')
    )
  }
});

答案 1 :(得分:0)

此答案也有效!

const banners = await db.banners.findAll({
      where: {
        region_id: id,
        status: "approved",
        approved_date: db.Sequelize.fn('CURRENT_DATE')

      }

    });