Rumah  >  Soal Jawab  >  teks badan

Bagaimanakah saya boleh melaksanakan pertanyaan ini dalam sekuel

<p><pre class="brush:php;toolbar:false;">SELECT * DARIPADA item_master DI MANA project_id DALAM (PILIH id DARI project_master WHERE workspace_id in (SELECT id FROM workspace_master WHERE company_id = 4));</pre> <p>Bagaimana untuk melaksanakan pertanyaan mysql ini dalam sekuel - Node js tanpa menggunakan pertanyaan asal? </p>
P粉354602955P粉354602955411 hari yang lalu450

membalas semua(2)saya akan balas

  • P粉245003607

    P粉2450036072023-09-06 00:38:24

    Jika struktur model anda serupa dengan item_master 有许多 project_master,并且 project_master 有许多 workspace_master,那么以下sequelize查询将与async/awaitSapukan bersama.

    Semasa saya mengedit jawapan saya. Seperti yang anda katakan dalam ulasan anda, anda mempunyai struktur model anda, cth. ruang kerja mempunyai banyak projek, projek mempunyai banyak projek. Kemudian pertanyaan Sequelize akan kelihatan seperti:

    const getResult = async (workpace_id, company_id = 4) => {
          const result = await workspace_master.findAll({
          subQuery: false,
          include: [
            {
              model: project_master,
              as: 'project_masters', // this is your alias defined in model
              attributes: ['id','foo','bar'],
              include: [
                {
                  model: item_master,
                  as: 'item_masters', // this is your alias defined in model
                  attributes: ['id','foo','bar']
                }
              ]
            }
          ],
          where: {
             id: workspace_id,
             company_id: company_id
          }
        });
        
        if(!result || !result.length) return res.send('Something went wrong!');
        return res.send(result);
       }

    Cuba sekarang, saya harap ini menyelesaikan masalah anda.

    balas
    0
  • P粉300541798

    P粉3005417982023-09-06 00:00:17

    const item = await Item.findAll({
        where: {
            status: { [Op.ne]: 99 },
            '$project.workspace.company_id$': { [Op.eq]: req.user.companyId }
        },
        include: {
            model: Project,
            as: 'project',
            attributes: ['id'],
            include: {
                model: Workspace,
                as: 'workspace',
                attributes: ['id', 'companyId'],
                where: {
                    companyId: req.user.companyId
                },
            },
        }
    })

    Ini berfungsi seperti yang saya mahu.

    balas
    0
  • Batalbalas