我正在尝试从MS Access在一个具有配置文件和许多用户的表中创建SQL查询,并能够识别哪些配置文件没有特定用户。
示例 我想返回不不包含用户 UserA
的个人资料个人资料表
Profile User
A UserA
A UserB
A UserC
A UserD
B UserB
B UserC
C UserA
D UserV
输出
Profile
B
D
答案 0 :(得分:2)
一种方法是group by
和having
:
select p.profile
from profile as p
group by p.profile
having sum(iif(p.user = "UserA", 1, 0)) = 0;
答案 1 :(得分:2)
您可以使用NOT EXISTS
:
select distinct profile from tablename as t
where not exists (
select 1 from tablename as tt
where tt.profile = t.profile and user = 'UserA'
)
答案 2 :(得分:1)
对于它的价值,您还可以使用联接:
select distinct t1.profile
from YourTable t1 left join
(select t2.profile as p from YourTable t2 where t2.user = "UserA") t3 on t1.profile = t3.p
where t3.p is null
或not in
:
select distinct t1.profile
from YourTable t1
where t1.profile not in (select t2.profile from YourTable t2 where t2.user = "UserA")
将YourTable
更改为表格名称。