RethinkDB - Left join where joined_table.identifier is null?

心不动则不痛 提交于 2020-01-23 21:11:41

问题


I have two tables in a RethinkDB database that represent a one-to-many relationship. Consider the following two tables:

t1
ParentId    Name
1           lorem
2           ipsum
3           dotor
4           sit
5           amet

t2
ChildId ParentId    ChildName
1       1           something
2       3           random
3       5           here

On t1, ParentId is the primary key, and on t2, there is a secondary index on ParentId. I want to find which parents don't have a child. The operation in SQL (MSSQL to be exact) looks like this:

SELECT t1.*
FROM t1
LEFT OUTER JOIN t2 ON t2.ParentId = t1.ParentId
WHERE t2.ChildId IS NULL

Results:
ParentId    Name
2           ipsum
4           sit

How can I accomplish this similar result in RethinkDB? Thank you!


回答1:


I would do it like this:

r.table('t1').filter(function(parent) {
  return r.table('t2').get_all(parent('ParentId'), {index: 'ParentId'}).count().eq(0);
})


来源:https://stackoverflow.com/questions/29872836/rethinkdb-left-join-where-joined-table-identifier-is-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!