如何在 Knex 上使用 EXISTS 进行子查询?
How can I make a subquery with EXISTS on Knex?
我正在尝试使用 knex 构建查询,但在如何使用条件 EXISTS (SELECT * FROM caregiver_patient WHERE patient_id IN (0,1))
.
实现 'WHERE' 部分时遇到了很多麻烦
这是 SQL 上的原始查询:
SELECT * FROM users
JOIN caregivers ON users.id = caregivers.user_id
JOIN caregiver_schedule ON caregivers.id = caregiver_schedule.caregiver_id
JOIN caregiver_patient ON caregivers.id = caregiver_patient.caregiver_id
JOIN patients ON caregiver_patient.patient_id = patients.id
WHERE caregiver_schedule.week_day = 2
AND caregiver_schedule.from_time <= 1320
AND caregiver_schedule.to_time > 1320
AND EXISTS (SELECT * FROM caregiver_patient WHERE patient_id IN (0,1));
这是我目前使用 Knex 的代码:
const caregivers = await db("caregivers")
.whereExists(function () {
this.select("caregiver_schedule.*")
.from("caregiver_schedule")
.whereRaw("`caregiver_schedule`.`caregiver_id` = `caregivers`.`id`")
.whereRaw("`caregiver_schedule`.`week_day` = ??", [Number(week_day)])
.whereRaw("`caregiver_schedule`.`from_time` <= ??", [timeInMinutes])
.whereRaw("`caregiver_schedule`.`to_time` > ??", [timeInMinutes]);
})
.join("users", "caregivers.user_id", "=", "users.id")
.join("patients", "caregiver_patient.patient_id", "=", "patients.id")
.select([
"caregivers.*",
"users.*",
"caregiver_schedule.*",
"patients.*",
]);
原始查询中的所有数字都应该是变量。 IN
里面的数字应该是一个数组。
谁能帮我完成在 Knex 上构建这个查询?
您提供的示例 knex 查询与原始 SQL 查询完全不相似,如果您想将其转换为,这就是原始 SQL 查询的样子knex 查询:
const result = await db('users')
.innerJoin('caregivers', 'users.id', 'caregivers.user_id')
.innerJoin('caregiver_schedule', 'caregivers.id' 'caregiver_schedule.caregiver_id')
.innerJoin('caregiver_patient', 'caregivers.id', 'caregiver_patient.caregiver_id')
.innerJoin('patients', 'caregiver_patient.patient_id', 'patients.id')
.where('caregiver_schedule.week_day', Number(week_day))
.where('caregiver_schedule.from_time', '<=', timeInMinutes)
.where('caregiver_schedule.to_time', '>', timeInMinutes)
.whereExists(function() {
this.select('*')
.from('caregiver_patient')
.whereIn('patient_id', [0,1]);
})
.select('*');
这假定 db
变量是您的 knex 连接。我还不得不猜测您想用来替换原始 SQL 中硬编码值的变量,随意用您喜欢的任何变量替换变量。
我正在尝试使用 knex 构建查询,但在如何使用条件 EXISTS (SELECT * FROM caregiver_patient WHERE patient_id IN (0,1))
.
这是 SQL 上的原始查询:
SELECT * FROM users
JOIN caregivers ON users.id = caregivers.user_id
JOIN caregiver_schedule ON caregivers.id = caregiver_schedule.caregiver_id
JOIN caregiver_patient ON caregivers.id = caregiver_patient.caregiver_id
JOIN patients ON caregiver_patient.patient_id = patients.id
WHERE caregiver_schedule.week_day = 2
AND caregiver_schedule.from_time <= 1320
AND caregiver_schedule.to_time > 1320
AND EXISTS (SELECT * FROM caregiver_patient WHERE patient_id IN (0,1));
这是我目前使用 Knex 的代码:
const caregivers = await db("caregivers")
.whereExists(function () {
this.select("caregiver_schedule.*")
.from("caregiver_schedule")
.whereRaw("`caregiver_schedule`.`caregiver_id` = `caregivers`.`id`")
.whereRaw("`caregiver_schedule`.`week_day` = ??", [Number(week_day)])
.whereRaw("`caregiver_schedule`.`from_time` <= ??", [timeInMinutes])
.whereRaw("`caregiver_schedule`.`to_time` > ??", [timeInMinutes]);
})
.join("users", "caregivers.user_id", "=", "users.id")
.join("patients", "caregiver_patient.patient_id", "=", "patients.id")
.select([
"caregivers.*",
"users.*",
"caregiver_schedule.*",
"patients.*",
]);
原始查询中的所有数字都应该是变量。 IN
里面的数字应该是一个数组。
谁能帮我完成在 Knex 上构建这个查询?
您提供的示例 knex 查询与原始 SQL 查询完全不相似,如果您想将其转换为,这就是原始 SQL 查询的样子knex 查询:
const result = await db('users')
.innerJoin('caregivers', 'users.id', 'caregivers.user_id')
.innerJoin('caregiver_schedule', 'caregivers.id' 'caregiver_schedule.caregiver_id')
.innerJoin('caregiver_patient', 'caregivers.id', 'caregiver_patient.caregiver_id')
.innerJoin('patients', 'caregiver_patient.patient_id', 'patients.id')
.where('caregiver_schedule.week_day', Number(week_day))
.where('caregiver_schedule.from_time', '<=', timeInMinutes)
.where('caregiver_schedule.to_time', '>', timeInMinutes)
.whereExists(function() {
this.select('*')
.from('caregiver_patient')
.whereIn('patient_id', [0,1]);
})
.select('*');
这假定 db
变量是您的 knex 连接。我还不得不猜测您想用来替换原始 SQL 中硬编码值的变量,随意用您喜欢的任何变量替换变量。