繁体   English   中英

Sequelize - 在 where 子句中查询其他表

[英]Sequelize - query other table inside where clause

我正在尝试做某事,但不知道这是否可以通过 sequelize 实现。 基本上我有这个代码片段在 graphQl 上运行,基本上它的作用是在 kits 表上找到 kits,然后验证“users”表上是否存在相同的 id。 如果不是,则返回它们,如果是,则不返回。 但是,现在我们需要对其进行扩展以进行分页,并且当前的代码段不是那么可扩展的。 这就是为什么我想在 where 子句中包含 for 循环或以某种方式检查那里,但真的不知道 mySql 上允许执行此操作的任何命令。

你有什么秘诀吗?

async findKitsWithResultNoReg2(_, {search}) {
      try {
        let promises = []
        const a1 = await db.kits.findAll({
          where: {
            [Op.and]: [
              { result: { [Op.or]: [1, 2, 3] } }, 
              { cp: 0 },
              {[Op.or]: [
                { kitID: { [Op.like]: '%' + search + '%' } }]}
            ]
          }
        })
        for (let i = 0; i < a1.length; i++) {
          const a2 = await db.users.findByPk(a1[i].dataValues.kitID)
          if (a2 === null) {
            const a3 = {
              kitID: a1[i].dataValues.kitID,
              result: a1[i].dataValues.result,
              date: a1[i].dataValues.resultDate
            }
            promises.push(a3)
          }
        }
        
        return Promise.all(promises)
      } catch (error) {
        console.log(error)
      }
    },
 async findKitsWithResultNoReg() {
      try {
        const a0 = await sequelize.query(`SELECT kitID, result, resultDate from kits where result in (1,2,3) and cp = 0 and archived = 0 and not Exists(select kitID from users where kits.kitID = users.kitID) order by resultDate desc`,  { type: QueryTypes.SELECT })
        const a1 = JSON.stringify(a0)
        return a1
      } catch (error) {
        console.log(error)
      }
    },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM