我有两个表:tbl_product、tbl_product_category
我想做一个API,它接受一个关键字,并返回与关键字匹配的所有产品和产品类别。如果结果来自tbl_product,那么它也返回它是产品。如果结果来自tbl_product_category,那么它也会返回它是类别。
tbl_product_catagory.findAll({
  raw: true,
  attributes: [[sequelize.literal("catagory_name"), "type"]],
  include: [{
    model: tbl_product,
    attributes: [[sequelize.literal(["product_name"]), "name"]],
    required: false,
    where: {
      product_name: {
        [Op.like]: "%" + data.word + "%"
      }
    }
  }]
})发布于 2021-03-10 01:12:31
我知道这很老了,但我喜欢使用nodejs来解决这个问题,因为sequelize还没有联合支持。非常简单,但有时我们没有意识到这一点:
示例:
Promise.all([
    Blog.findAll({ include: { model: Author, required: true } }),
    Blog.findAll({ include: { model: AnonymInfo, required: true } }),
]).then((modelReturn) => resolve(modelReturn.flat()))https://stackoverflow.com/questions/50986817
复制相似问题