我有表:
人表
ID NAME
1 Matt
2 Josh
Run Code Online (Sandbox Code Playgroud)
文件表
FILE_ID PERSON_ID TYPE
1 1 Photo
2 2 Text
3 2 Text
Run Code Online (Sandbox Code Playgroud)
我想返回所有在另一个表中没有照片类型的文件的人。
在上述情况下,我想返回:
ID NAME
2 Josh
Run Code Online (Sandbox Code Playgroud)
执行此操作的最有效方法是什么?
我会用not exists:
select p.*
from person p
where not exists (select 1
from files f
where f.person_id = p.id and f.type = 'Photo'
);
Run Code Online (Sandbox Code Playgroud)
为了提高性能,您需要在files(person_id, type).
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |