我有条件:如果d ate_work <= report_date
,则显示结果。
现在,如何从结果中获得最大日期,即date_work
?
module.exports.getAll = async function (req, res) {
try {
const query = {
distinct: true,
subQuery: false,
offset: +req.query.pageSize * (+req.query.page - 1),
limit: +req.query.pageSize,
include: [{
model: Teams,
include: [{
model: Masters,
where: {
[Op.or]: [
Sequelize.where(
Sequelize.col("date_work"),
{
[Op.lte]: Sequelize.col("rep_4hours.report_date")
}
)
]
},
required: true
}],
required: true
}]
};
const rep4Hours = await Rep4Hours.findAndCountAll(query);
res.status(200).json(rep4Hours);
} catch (e) {
errorHandler(res, e);
}
}
您必须在查询中添加属性和分组依据。
attributes: [[sequelize.fn('max', sequelize.col('date_work')), 'max_date_work']],
group:['date_work'],
raw: true
组列可以根据您的数据库而有所不同。