Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用join少表获取不同的数据_Node.js_Sequelize.js - Fatal编程技术网

Node.js 使用join少表获取不同的数据

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方面没有太多经验。这是我现在尝试的,它返回米饭和膳食数据

这是关于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));
    

哇。。。。它正在工作。。。谢谢但我担心的是,上面的查询是否会影响性能,在增加数据库时,会导致where条件中存在“IN”。当我们使用SQL执行此操作时,我们不需要任何类型的“IN”子句,只需要使用内部连接和不同的关键字即可。