Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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
Javascript 在续集里我该怎么做_Javascript_Sql_Sequelize.js - Fatal编程技术网

Javascript 在续集里我该怎么做

Javascript 在续集里我该怎么做,javascript,sql,sequelize.js,Javascript,Sql,Sequelize.js,我有一个疑问: WITH tempA AS (SELECT conversationId FROM participant WHERE userId = "aaa"), tempB AS (SELECT conversationId FROM participant WHERE userId = "bbb") SELECT conversationId FROM tempA, tempB WHERE tempA.conversationId = tempB.conversation

我有一个疑问:

WITH tempA AS (SELECT conversationId FROM participant WHERE userId = "aaa"),
     tempB AS (SELECT conversationId FROM participant WHERE userId = "bbb")
SELECT conversationId 
FROM tempA, tempB 
WHERE tempA.conversationId = tempB.conversationId;
查询将返回两个用户都参与的对话的id

我在Sequelize中也有一个参与模式:

const Participation = sequelize.define("participation", {
    //...attributes
});

module.exports = Participation;
如何在Sequelize中执行上述查询而不使用
Sequelize.query

您可以使用来获得相同的效果。下面是一个粗略的例子:

   /* for the ON clause of JOIN */
   participation.hasMany(participation, {
      sourceKey : 'userId',
      foreignKey: 'userId',
      as: 'selfJoin'
      });

   participation.addScope('tempA', {
      attributes: ['message_id'],
      where: {userId: 'aaa'}
      });

  participation.addScope('tempB',{
      attributes: ['message_id'],
      where: {userId: 'bbb'}
      });


   participation.scope('tempB').findAll({    
      attributes: ['message_id'],
      include: [{
         model: participation.scope('tempA'),
         required: true,
         as: 'selfJoin',
         attributes: []
        }]
     });