我需要根据年龄过滤“进入者”,但是在表中我只有出生日期。我需要的年龄介于最低年龄和最高年龄之间,该怎么办?
router.post('/', function (req, res, next) {
let parameters = JSON.parse(req.body.parameters);
parameters.states = parameters.states.split();
parameters.species = parameters.species.split();
model.person.findAll({
where: {
uf: parameters.states,
species: parameters.species,
dateBirth: BETWEEN PARAMETERS.MINAGE AND PARAMETERS.MAXAGE
}
})
.then(person => res.json({
error: false,
data: person
}))
.catch(error => res.json({
error: true,
data: [],
error: error
}));
});
答案 0 :(得分:0)
您可以使用between
运算符:
model.person.findAll({
where: {
uf: parameters.states,
species: parameters.species,
dateBirth: {
[Op.between]: ['2000-01-01', '2020-01-01'],
}
}
})
答案 1 :(得分:0)
您可以像这样使用它:
var startAge = 18;
var endAge = 20;
var today = moment();
var startDate = today.subtract('years', startAge);
var endDate = today.subtract('years', endAge);
model.person.findAll({
where: {
uf: parameters.states,
species: parameters.species,
dateBirth: : {
$between: [startDate, endDate]
}
}
})
答案 2 :(得分:0)
您可以在类似-
的位置使用Raw查询var Sequelize = require("sequelize");
var now = '2010-01-01';//Current Date
model.person.findAll({
where: Sequelize.literal('( (ud IN ('+parameters.states+')) AND (species IN ('+parameters.species+')) AND ((DATE_FORMAT(FROM_DAYS(DATEDIFF(dob,'+now+')), "%Y")+0) BETWEEN '+minDate+' AND '+maxDate+') )')
})