问题
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