在Sequelize中制作此SQL

时间:2019-01-15 22:35:38

标签: javascript node.js sequelize.js

我尝试在My Sequelize控制器中创建此SQL,但是我不知道data_time看起来只有<30天。

SQL:

SELECT * FROM bankapplication_transactions
WHERE id_sender = 1
OR id_recipient = 1
AND data_time BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
ORDER BY data_time DESC

Sequelize控制器:

Transaction.findAll({
              where: db.Sequelize.and(
                {
                  data_time: moment().add(-30, 'days'), // this is ok?
                },
                db.Sequelize.or(
                  { id_sender: 1 },
                  { id_recipient: 1 },
                ),
              ),

              order: ['data_time', 'DESC'],
            }).then(transaction_history => {
              if (transaction_history) {
                console.log('transaction_history', transaction_history);
              }
            });

1 个答案:

答案 0 :(得分:0)

var today, afterMonth = new Date();
afterMonth.setMonth(today.getMonth() + 1);

...

where: db.Sequelize.and(
            {
              data_time:  $between: [today , afterMonth ]
            },