首页 > 解决方案 > 在 Sequelize 中创建此 SQL

问题描述

我尝试在我的 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

续集控制器:

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);
              }
            });

标签: javascriptnode.jssequelize.js

解决方案


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

...

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

推荐阅读