gpt4 book ai didi

javascript - 使用两个 where 条件运行 sequelize

转载 作者:行者123 更新时间:2023-12-03 22:19:17 25 4
gpt4 key购买 nike

我有一个 mysql db 实例,其中包含一个由各种字段组成的表。相关字段是开始、开始时间和状态

  • 开始:YYYY-MM-DD
  • 开始时间:HH:mm:ss
  • 状态:ENUM('canceled', 'scheduled, etc)

  • 如果我想获取所有没有 status = 'cancelled' 并且发生在今天或之后的条目的列表,我会这样写:
      return {
    where: {
    status: {
    $ne: 'cancelled'
    },
    $or: {
    start: { $gte: moment().utc().format('YYYY-MM-DD') },
    $and: {
    isRepeating: 1,
    $or: [{
    end: {
    $gte: moment().format(),
    }
    },
    {
    end: {
    $eq: null,
    }
    }]
    },
    }
    },
    我正在尝试修改此查询,不仅为我提供今天或之后发生的条目,而且还比现在更大(时间明智,UTC)。我的尝试是先根据 s​​tartTime 过滤,然后根据 startDate 过滤,但它似乎不起作用:
        return {
    where: {
    status: {
    $ne: 'cancelled'
    },
    $or: {
    startTime: { $gt: moment.utc().format('HH:mm:ss') },
    $and: {

    start: { $gte: moment().utc().format('YYYY-MM-DD') },

    $and: {
    isRepeating: 1,
    $or: [{
    end: {
    $gte: moment().format(),
    }
    },
    {
    end: {
    $eq: null,
    }
    }]
    }
    },
    }
    },
    (不起作用,因为它只是返回所有内容!)
    我也不能做更简单的事情
    where: {
    startTime: { $gt: moment.utc().format('HH:mm:ss') },
    start: { $gte: moment().utc().format('YYYY-MM-DD') },
    }
    因为它会忽略例如明天发生的条目,但发生在当天比当前时间戳更早的日期。
    谢谢!

    最佳答案

    您可以使用 Op.and 运算符来组合这些条件。

    const { Op } = require("sequelize");

    ...
    where: {
    [Op.and]: [
    startTime: { $gt: moment.utc().format('HH:mm:ss') },
    start: { $gte: moment().utc().format('YYYY-MM-DD') }
    ]
    }
    ...

    关于javascript - 使用两个 where 条件运行 sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64345780/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com