0
我使用nodejs工作後綴,我嘗試將我的查詢代碼從mysql查詢轉換爲sequelize語法你能幫我解決這個問題嗎?將查詢從mysql客戶端轉換爲續集語法
MySQL查詢:
select username,message,fromUserId,toUserID from messages inner join messageToUsers on messages.id = messageToUsers.messageID left outer join users on messages.fromUserID = users.id where messageToUsers.`toUserID` = 5 and messages.`fromUserId` = 7 or messageToUsers.`toUserID` = 7 and messages.`fromUserId` = 5
sequlize查詢
Message.hasMany(MessageToUser,{foreignKey:'messageID'});
Message.belongsTo(Users,{foreignKey:'fromUserId'});
Message.findAll({where:{fromUserId :5,fromUserID : 7},
include :[
{model:MessageToUser,where:{toUserID:5,toUserID:7}} ,
{model:Users,attributes:['id','username']}
]
}).then(function(messages) {
console.log(JSON.stringify(messages));
res.status(200).json(messages);
})