【问题标题】:Left join query with rethinkDB使用 rethinkDB 的左连接查询
【发布时间】:2018-05-02 10:27:22
【问题描述】:

我有两张桌子,usersauthors。 用户可以是作者,如果是,则在作者文档中有用户的id

我要查询的是所有不是作者的用户。 等效的 mySQL 查询类似于

select * from users left join authors on users.id=authors.userId
where authors.userId is null

我尝试的是

r.db('journa').table('authors').outerJoin(
  r.db('journa').table('users'),
  (author, user) => {
    return author.hasFields({'left': 'claimedByUserId'}).not()
  }
)

但它不起作用。 关于如何实现它的任何想法?

【问题讨论】:

    标签: rethinkdb rethinkdb-javascript


    【解决方案1】:

    你几乎是在正确的方式。您不应该在连接表达式中反转,因为它将返回一组所有不匹配的排列,并且您将无法检测到所需的匹配。以下查询似乎可以满足您的需求:

    r.db('journa')
        .table('users')
        .outerJoin(
            r.db('journa').table('authors'),
            // This is what two sequences are joined upon
            (user, author) => user('id').eq(author('userId'))
        )
        // Joins return `left` and `right`.
        // If there's nothing at the right side, we assume there was no match
        .filter((lr) => lr.hasFields('right').not())
        // Extracting the left table record only
        .map((lr) => lr('left'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 2015-09-17
      • 2021-12-25
      • 2017-06-30
      相关资源
      最近更新 更多