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: []
}]
});