我正在尝试做外部联接三个表。我的数据库是sakila数据库。
我正在尝试获取从未租借的电影标题行。所以,我加入了三个这样的表。
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
match /ar/{organisationDoc} {
allow read: if request.auth.uid == userId;
allow write: if false;
}
}
// read
match /organisations/{oId} {
allow get: if exists(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId));
allow list: if request.auth.uid in resource.data.users;
match /{all=**} {
allow get: if exists(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId));
allow list: if request.auth.uid in resource.data.users;
}
}
// write
match /organisations/{oId} {
allow create: if get(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId)).data.create == true;
allow update: if get(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId)).data.update == true;
allow delete: if get(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId)).data.delete == true;
match /{all=**} {
allow create: if get(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId)).data.create == true;
allow update: if get(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId)).data.update == true;
allow delete: if get(/databases/$(database)/documents/users/$(request.auth.uid)/ar/$(oId)).data.delete == true;
}
}
}
}
它返回行,但问题是我不知道此查询是对还是错。有人可以帮我吗?
答案 0 :(得分:0)
我认为您应该使用an EXISTS
clause instead as this allows a sub-query并测试是否返回行:
SELECT f.title
FROM film f
JOIN inventory i ON f.film_id = i.film_id
WHERE NOT EXISTS (
SELECT 1
FROM rental r
WHERE i.inventory_id = r.inventory_id
)
答案 1 :(得分:0)
您正在从胶片表中选择胶片。您是外部库存和租金。然后,您保留所有NULL租金。这就是所谓的反联接,这是弱DBMS用来模仿NOT EXISTS
或NOT IN
子句的一种技巧,这些DBMS无法很好地执行。
该查询将为您提供所有根本没有库存或没有库存但没有单次出租的电影。或简短:没有租金的电影。正是您想要实现的目标。
编写查询的另一种方法是:
select title
from film f
where not exists
(
select *
from inventory i
join rentals r on r.inventory_id = i.inventory_id
where i.film_id = f.film_id
);