Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use datediff of mysql with sequelizejs

I'm trying to implement this SQL to my Sequelize

SELECT file_id FROM table WHERE datediff(curdate(),create_date) > 5;

   Here is my Sequelize

   findOverPeriodFile: function () {
    return table.findAll({
        where:{
            60: {lt: Sequelize.fn('')}
        }
    });
}

Im new to Seuelize and I have tried to search Google, but it doesn't help. Does anyone have an answer to this question? I don't know what to put in the WHERE statement. Sorry for my bad English.

like image 858
Nguyen Hoang Vu Avatar asked Mar 06 '23 18:03

Nguyen Hoang Vu


1 Answers

Here you go :

You can generate WHERE datediff(curdate(),create_date) > 5; by using :

{ 
    where: sequelize.where(sequelize.fn('datediff', sequelize.fn("NOW") , sequelize.col('create_date')), {
        $gt : 5 // OR [Op.gt] : 5
    })
}

For adding more condition

{
    where: {
        $and : [
            sequelize.where(sequelize.fn('datediff', sequelize.fn("NOW") , sequelize.col('create_date')), {
                $gt : 5 // OR [Op.gt] : 5
            }) ,
            { status : 'mystatus' }
        ]
    }
} 
like image 95
Vivek Doshi Avatar answered Mar 16 '23 03:03

Vivek Doshi