Reputation: 23
I currently have this code:
Evento.findAll({
where: db.sequelize.where(db.sequelize.fn('MONTH', db.sequelize.col('start_date')), req.body.month),
order: ['start_date'],
attributes: ['id', 'description', 'start_date', 'end_date', 'picture', 'status', 'place', 'text'],
include: [{
model: TypeEvent
}]
}).then(events => {
if (events) {
res.status(200).send({
events
});
} else {
res.status(400).send({
message: "without events"
});
}
});
but I also need to consult for the year as well and I don't know how to do it, I have tried in all the ways but I can't get the answer.
The SQL query I want :
SELECT .... FROM ... WHERE MONTH(start_date)=5 AND YEAR(start_date)=2021 ORDER BY ..
Help me please¡¡¡¡
Upvotes: 2
Views: 4246
Reputation: 4434
Evento.findAll({
where: {
[Op.and]: [
Sequelize.where(Sequelize.fn('MONTH', Sequelize.col('start_date')), req.body.month),
Sequelize.where(Sequelize.fn('YEAR', Sequelize.col('start_date')), req.body.year),
],
},
order: ['start_date'],
attributes: ['id', 'description', 'start_date', 'end_date', 'picture', 'status', 'place', 'text'],
include: [
{
model: TypeEvent,
},
],
}).then((events) => {
if (events) {
res.status(200).send({
events,
});
} else {
res.status(400).send({
message: 'without events',
});
}
});
Upvotes: 7