Sequelize.js 如何筛选sequelize上的关联表或父级

Sequelize.js 如何筛选sequelize上的关联表或父级,sequelize.js,Sequelize.js,如何筛选sequelize上的子(关联)表或父表 我想按子表的创建日期选择数据,但若父表并没有子表,我想按父表的创建日期选择 所以我需要过滤器来比较父字段和子字段 例如: 有没有一种方法可以在不使用文本的情况下使用sequelize执行以下查询 SELECT * FROM owner, car WHERE (car.owner_id IS NULL AND car.created_at=NOW()) OR (car.owner_id IS NOT NULL AND owner.created_a

如何筛选sequelize上的子(关联)表或父表

我想按子表的创建日期选择数据,但若父表并没有子表,我想按父表的创建日期选择

所以我需要过滤器来比较父字段和子字段

例如: 有没有一种方法可以在不使用文本的情况下使用sequelize执行以下查询

SELECT * FROM owner, car
WHERE (car.owner_id IS NULL AND car.created_at=NOW())
OR (car.owner_id IS NOT NULL AND owner.created_at=NOW())
我无法在Op.OR中添加包含

我也尝试了下面的代码,但是它只考虑了父的CeRealIdIt字段:

where: {
  [Op.or]: {
    [Op.and]: {
      owner_id: null,
      created_at: {
        [Op.between]: [startOfDay(searchDate), endOfDay(searchDate)],
      },
    },
    [Op.and]: {
      owner_id: { [Op.not]: null },
      created_at: {
        [Op.between]: [startOfDay(searchDate), endOfDay(searchDate)],
      },
    }
  }
}

谢谢

考虑到您的车型名称是
汽车
车主

Owner.findAll({
  include: [
    {
      model: Car
    }
  ],
  attributes: ['some_attribute', 'another_attribute'], //Filter attributes
  where: {
    [Op.or]: {
      [Op.and]: {
        '$Car.owner_id$': { [Op.is]: null },
        '$Car.created_at$': {
          [Op.between]: [startOfDay(searchDate), endOfDay(searchDate)],
        },
      },
      [Op.and]: {
        '$Car.owner_id$': { [Op.not]: null },
        created_at: { // This attribute is referencing owner's created_at
          [Op.between]: [startOfDay(searchDate), endOfDay(searchDate)],
        },
      }
    }
  }
})

您可以在where子句中使用“$$”字符串引用
Car
,看看它是否适合您。

考虑到您的车型名称是
Car
Owner

Owner.findAll({
  include: [
    {
      model: Car
    }
  ],
  attributes: ['some_attribute', 'another_attribute'], //Filter attributes
  where: {
    [Op.or]: {
      [Op.and]: {
        '$Car.owner_id$': { [Op.is]: null },
        '$Car.created_at$': {
          [Op.between]: [startOfDay(searchDate), endOfDay(searchDate)],
        },
      },
      [Op.and]: {
        '$Car.owner_id$': { [Op.not]: null },
        created_at: { // This attribute is referencing owner's created_at
          [Op.between]: [startOfDay(searchDate), endOfDay(searchDate)],
        },
      }
    }
  }
})

您可以在where子句中使用“$$”字符串引用
Car
,看看它是否适合您。

谢谢您,费尔南多!成功了!但是我们必须对上面的代码做一点修改。我们必须删除Op和。谢谢你,费尔南多!成功了!但是我们必须对上面的代码做一点修改。我们必须删除Op和。