我需要得到这样一个复杂的查询。检查用户对 table1 数据的访问。使用 ORM Sequelize js。
SELECT *
FROM table1
WHERE id = 23
AND "table2_id" IN
(SELECT id
FROM "table2"
WHERE "table3_id" IN
(SELECT id
FROM "table3"
WHERE "table_4" IN
(SELECT id
FROM table4
WHERE "table5_id" IN
(SELECT "id"
FROM "table5"
WHERE "userId" = 1))))
我尝试使用包含参数const table1_item = await Table1.findOne({
where: {id: id, '$table2.table3.table4.table5.user.id$': user.id},
include: [
{
model: Table2,
attributes: [],
include: [{
model: Table3,
attributes: [],
include: [{
model: Table4,
attributes: [],
include: [{
model: Table5,
attributes: [],
include: [{
model: User,
attributes: [],
}]
}]
}]
}]
}
]
});
但它使用 JOIN 生成查询。并且 DB 正在慢慢返回多行数据。
最佳答案
这里唯一的选择是 Sequelize.literal
中的 where
。有关类似问题,请参阅我的 answer
关于javascript - Sequelize 生成复杂查询 (SELECT WHERE IN),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63763602/