我正在为类似 Tinder 的项目使用 node.js
框架、Sails JS 及其内置的 ORM Waterline。
我不知道如何编写查询来获取未连接的记录。就我而言,当用户“喜欢”或“不喜欢”另一个用户的个人资料时,我希望不再显示喜欢的个人资料。
我该如何实现?到目前为止,这是我尝试过的:
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);
});
最佳答案
如果我没听错,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';
用于更清晰 View 的纯 SQL:
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 <>当前用户Id
的记录关于mysql - 如何查找未与 Waterline ORM 连接的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33351315/