如何在续集查询方法中将时间戳与当前日期进行比较

时间:2017-07-06 06:59:01

标签: sequelize.js

如何以续集的方式编写此查询:

select COUNT(*)
    from r5.users
WHERE
    DATE_FORMAT(createdAt,'%Y%c%d')=DATE_FORMAT(current_date(),'%Y%c%d')

1 个答案:

答案 0 :(得分:0)

你可以这样做,

Model.users.count({
where: Model.Sequelize.where(
    Model.Sequelize.fn('DATE_FORMAT',Model.Sequelize.col('createdAt'),"%Y%c%d"),
    {like:Model.Sequelize.fn('DATE_FORMAT',Model.Sequelize.fn('current_date'),"%Y%c%d")})
});

Sequelize.fn可用于调用数据库函数,它需要Sequelize.fn(functionToCall, argumentsToPassToIt)

形式的语法

Sequelize.Col用于引用特定列,例如createdAt

Sequelize.where可用于向要添加的列添加函数