【问题标题】:How to find records that are not joined with Waterline ORM如何查找未与 Waterline ORM 连接的记录
【发布时间】:2016-01-25 21:08:34
【问题描述】:

我正在将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);
    });

【问题讨论】:

  • 如何在没有任何示例代码的情况下帮助您编写查询?

标签: mysql node.js sails.js waterline


【解决方案1】:

如果我没记错的话, 是用户个人资料吗? 所以,我们需要配置文件:

  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';
    

纯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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2022-09-27
    • 2014-03-18
    • 2020-05-11
    • 2015-08-25
    相关资源
    最近更新 更多