Node.js 使用join少表获取不同的数据
这是关于sequelize ORM的,我正在为这个应用程序开发NodeJSAPI。 米饭桌(FK:mealId)链接到餐桌(PK:Id),餐桌(FK:RestaurantId)链接到餐厅餐桌(PK:Id)。没有从“mealprice”表直接连接到“restaurant”表。 我需要的是,如果我将“IsActive=true”状态传递到mealPrice表,并且应该得到餐厅(不同列表)列表。老实说,我在sequelize ORM方面没有太多经验。这是我现在尝试的,它返回米饭和膳食数据Node.js 使用join少表获取不同的数据,node.js,sequelize.js,Node.js,Sequelize.js,这是关于sequelize ORM的,我正在为这个应用程序开发NodeJSAPI。 米饭桌(FK:mealId)链接到餐桌(PK:Id),餐桌(FK:RestaurantId)链接到餐厅餐桌(PK:Id)。没有从“mealprice”表直接连接到“restaurant”表。 我需要的是,如果我将“IsActive=true”状态传递到mealPrice表,并且应该得到餐厅(不同列表)列表。老实说,我在sequelize ORM方面没有太多经验。这是我现在尝试的,它返回米饭和膳食数据
global.mealpricing.belongsTo(global.meal, { targetKey: 'id', foreignKey: 'mealId' });
global.meal.belongsTo(global.resturant, { targetKey: 'id', foreignKey: 'resturantId' });
global.mealpricing.findAll({
include: [{
model: global.meal,
}],
where: { isActive: true }
}).then(meals => {
res.send(meals);
});
我关心的是如何获得唯一的餐厅列表?请给我一个方向。- 使用mealpricing与餐桌的内部联接获取所有餐厅ID
- 使用“id”列上的distinct运算符,通过这些id查找所有餐厅
global.mealpricing.findAll({ where: {isActive: true}, include: { model: global.meal, attributes: ['resturantId'] } }) .then(mealPricingDocs => { const restaurantIds = mealPricingDocs.map(i => i.meal.restaurantId); return global.restaurant.findAll({ where: { id: { $in: restaurantIds } }, distinct: 'id' }); }) .then(restaurants => res.send(restaurants));