如何在 prisma 客户端中检索具有相同表的子查询?

Ily*_*ath 4 postgresql node.js prisma prisma-graphql

select * 
from WeeklyChallengeCourses 
where weekly_challenge_id = (select weekly_challenge_id 
                             from WeeklyChallengeCourses 
                             where course_id = 210);
Run Code Online (Sandbox Code Playgroud)

结果将是以下选定的一项:

在此输入图像描述

const data = await context.prisma.weeklyChallengeCourses.findMany({
        where:{
            weekly_challenge_id: {
                ..............
            }
        },
    });
Run Code Online (Sandbox Code Playgroud)

Tas*_*mam 7

在 Prisma 中,您必须使用两个不同的查询来解决此问题:

  1. 运行子查询的等效项来获取weekly_challenge_id
  2. findMany使用weekly_challenge_id步骤 1 中找到的结果运行 a 。
// I'm assuming course_id is unique. 
const course = await context.prisma.findUnique({ where: { course_id: 210 } }); 

const data = await context.prisma.weeklyChallengeCourses.findMany({
        where:{
            weekly_challenge_id: course.weekly_challenge_id
        },
    });
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用该rawQuery功能 直接运行 SQL 并在一个查询中完成。