解决Sequelize查询遇到的问题
前言
这篇文章可能只适合我这个脑残来记录了,这两天我遇到了一个问题,将传入时间和表中的时间进行比较,但是这个where 就是主表下面的关联表中,但有个问题就是主表下的关联表数据不是全部都有的,存在无数据的情况,所以这个导致我where条件不生效。以上属于废话,我都有点语无伦次了。
我要实现的就是查询在传入的时间没有请假的导游并且没有订单的导游
查主表全部数据及include(订单表及请假表)
let include = [
{
model: this._Indent,
as: 'indent',
required: false
},
{
model: this._TourGuideLeave,
as: 'tour_guide_leaves',
required: false
}
]
const AllTourGuide = await this._TourGuide.findAll({
include,
where
})
定义一个数组,然后遍历不满足条件的,然后返回满足的条件
let arr1 = []
for (let result of AllTourGuide) {
let state = true // 给全部导游定义一个标签值为true
for (let indents of result.indent) { // 遍历导游的订单
if (indents.startTime <= Time && indents.endTime >= Time) { // 满足条件则标签值为false
state = false
}
}
if (state) {
for (let leaves of result.tour_guide_leaves) { // 遍历导游请假
if (leaves.startAt <= Time && leaves.endAt >= Time) { // 满足条件则标签值为false
state = false
}
}
}
if (state) { // 则剩余的就是可被预约的导游
arr1.push(result)
}
}