我正在使用node.js
框架,Sails JS及其内置的ORM Waterline来进行类似Tinder的项目。
我无法想象如何编写查询以获取未加入的记录。在我的情况下,当用户“喜欢”或“不喜欢”其他用户的个人资料时,我希望喜欢的个人资料不再显示。
我如何做到这一点?这是我到目前为止所尝试的:
Cat.query('select cat.* from cat left outer join vote on cat.id = vote.owner where vote.owner is null AND cat.id !='+req.session.User.id, function (err, results) {
if (err) return res.serverError(err);
res.send('Yes');
console.log(results);
});
答案 0 :(得分:1)
如果我说得对, Cat 是用户个人资料? 因此,我们需要以下内容:
cat.id !== req.session.User.id
)当前用户不喜欢或不喜欢。
var query = 'select cat.* ' +
' from cat left join vote on' +
// joining votes for current user
' cat.id = vote.catId and vote.owner = ' + req.session.User.id +
' where' +
// filtering out the Cat with current user Id (if needed)
' cat.id <> ' + req.session.User.id +
' and' +
// leaving only records with empty vote id(meaning there's no corresponding vote record)
' vote.id is null';
Pure SQL用于更多crear视图:
select cat.* from cat left join vote on
cat.id = vote.catId and vote.owner = :userId
where
cat.id <> :userId
and
vote.id is null
我们从 cat 表中选择所有记录,加入当前用户为他们所做的投票。然后只留下缺少选票的记录,以及id&lt;&gt;当前用户标识