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

标签 mysql node.js graphql sequelize.js

我正在尝试做一些事情,但不知道 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)
      }
    },

关于mysql - Sequelize - 在 where 子句中查询其他表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66704089/

相关文章:

mysql - 确定 MySQL 行的存储要求

node.js - Electron -node.js-ng : file path\ng. ps1 无法加载,因为在此系统上禁用运行脚本

node.js - 如何使用Gun作为快速路线?

GraphQL 变异变量

php - 将多个文件上传到新创建的目录

php - 我如何循环遍历 mysql 中的值并随时使用它们

javascript - Nodejs 对象声明和立即速记 if 语句使应用程序崩溃

graphql - 记录 GraphQL API

javascript - 使用不同的模板在 gatsby 中创建页面

php - php 和 codeigniter 中的主要类别 + 3 级子类别