mysql - 如何查找未与 Waterline ORM 连接的记录

标签 mysql node.js sails.js waterline

我正在为类似 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 是用户个人资料吗? 因此,我们需要配置文件:

  1. 不属于当前用户(cat.id !== req.session.User.id)
  2. 当前用户不喜欢或不喜欢。

    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/

相关文章:

mysql - 加入后 SUM 不正确

json - Sails.js/Nodejs/Json 对表情符号的支持

node.js - Sails 与 Loopback API 后端

php - 根据名字的第一个字母对 PHP 结果进行分组

mysql - 这种方法对于提高 MySQL 性能是好还是坏?

php - 将数组数据插入 mysql 表的高效 php 代码?

node.js - 使用node.js和mongoose以及expressjs和passportjs进行身份验证

node.js - 如何在 Windows 中手动安装 npm?

javascript - NodeJS 中的 varexports = module.exports = {}

node.js - 如何使用sails.js 为模型定义实例方法