如何获取最近 7 天的所有记录 sequelize
How to get all records from the last 7 days sequelize
我正在尝试使用 sequelize 从 mysql 数据库中获取所有记录,我尝试了以下方法:
shops.findAndCountAll({
where: {
createdAt: {
[Op.gte]: moment().subtract(7, 'days').toDate()
}
}
})
当我使用它时,出现错误:
ReferenceError: moment is not defined
所以我尝试了这个方法:
shops.findAndCountAll({
where: {
createdAt: {
[Op.gte]: Sequelize.literal('NOW() - INTERVAL "7d"'),
}
}
})
但是我得到以下错误
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '' at line 1",
sql: "SELECT count(*) AS `count` FROM `shop` AS `shops` WHERE `shops`.`createdAt` >= NOW() - INTERVAL '7d';"
},
sql: "SELECT count(*) AS `count` FROM `shop` AS `shops` WHERE `shops`.`createdAt` >= NOW() - INTERVAL '7d';"
}
我该如何解决这个问题。我不介意我使用哪种方法,只要我让它起作用即可。
提前致谢
您没有导入 moment
;因此 moment
未定义。
尝试
const moment = require('moment') //<es6
或
import moment from 'moment'
使用 DATE_ADD() 或 DATE_SUB()
SELECT * FROM Table_Name
WHERE connect_time >= DATE_ADD(CURDATE(),INTERVAL -7 DAY);
或
SELECT * FROM Table_Name
WHERE connect_time >= DATE_SUB(CURDATE(),INTERVAL 7 DAY);
没有这些功能,你也可以
SELECT * FROM Table_Name
WHERE connect_time >= (CURDATE() + INTERVAL -7 DAY);
或
SELECT * FROM Table_Name
WHERE connect_time >= (CURDATE() - INTERVAL 7 DAY);
我正在尝试使用 sequelize 从 mysql 数据库中获取所有记录,我尝试了以下方法:
shops.findAndCountAll({
where: {
createdAt: {
[Op.gte]: moment().subtract(7, 'days').toDate()
}
}
})
当我使用它时,出现错误:
ReferenceError: moment is not defined
所以我尝试了这个方法:
shops.findAndCountAll({
where: {
createdAt: {
[Op.gte]: Sequelize.literal('NOW() - INTERVAL "7d"'),
}
}
})
但是我得到以下错误
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '' at line 1",
sql: "SELECT count(*) AS `count` FROM `shop` AS `shops` WHERE `shops`.`createdAt` >= NOW() - INTERVAL '7d';"
},
sql: "SELECT count(*) AS `count` FROM `shop` AS `shops` WHERE `shops`.`createdAt` >= NOW() - INTERVAL '7d';"
}
我该如何解决这个问题。我不介意我使用哪种方法,只要我让它起作用即可。
提前致谢
您没有导入 moment
;因此 moment
未定义。
尝试
const moment = require('moment') //<es6
或
import moment from 'moment'
使用 DATE_ADD() 或 DATE_SUB()
SELECT * FROM Table_Name
WHERE connect_time >= DATE_ADD(CURDATE(),INTERVAL -7 DAY);
或
SELECT * FROM Table_Name
WHERE connect_time >= DATE_SUB(CURDATE(),INTERVAL 7 DAY);
没有这些功能,你也可以
SELECT * FROM Table_Name
WHERE connect_time >= (CURDATE() + INTERVAL -7 DAY);
或
SELECT * FROM Table_Name
WHERE connect_time >= (CURDATE() - INTERVAL 7 DAY);