Postgresql排除查询
一般来说,我对Postgresql和SQL比较陌生。我已经在这个问题上坐了一段时间,不知道如何编写查询来获得我想要的结果 我有这个示例表 使用者 名称 支付 A. aaa 真的 B bbb 真的 C ccc 假的 D aaa 假的 E eee 假的Postgresql排除查询,sql,postgresql,Sql,Postgresql,一般来说,我对Postgresql和SQL比较陌生。我已经在这个问题上坐了一段时间,不知道如何编写查询来获得我想要的结果 我有这个示例表 使用者 名称 支付 A. aaa 真的 B bbb 真的 C ccc 假的 D aaa 假的 E eee 假的 一种方法使用的不存在: select t.* from t where not paid and not exists (select 1 from t t2 whe
一种方法使用的
不存在
:
select t.*
from t
where not paid and
not exists (select 1
from t t2
where t2.name = t.name and t2.paid
);
当然,外部查询中的未支付
是多余的。因此,您可以使用:
select t.*
from t
where not exists (select 1
from t t2
where t2.name = t.name and t2.paid
);
一种方法使用的
不存在
:
select t.*
from t
where not paid and
not exists (select 1
from t t2
where t2.name = t.name and t2.paid
);
当然,外部查询中的未支付
是多余的。因此,您可以使用:
select t.*
from t
where not exists (select 1
from t t2
where t2.name = t.name and t2.paid
);
您可以使用中的子查询排除名称:
SELECT
*
FROM mytable
WHERE "Name" NOT IN ( -- 2
SELECT "Name" FROM mytable WHERE "Paid" = true -- 1
)
获取所有付费记录的名称
获取它们并过滤所有相关记录
您可以使用中的子查询排除名称:
SELECT
*
FROM mytable
WHERE "Name" NOT IN ( -- 2
SELECT "Name" FROM mytable WHERE "Paid" = true -- 1
)
获取所有付费记录的名称
获取它们并过滤所有相关记录
您可以使用“不存在”,如下所示:
模式:
查询:
输出:
用户
名称
支付
u_519553787
ccc
F
u_519553787
eee
F
您可以使用“不存在”,如下所示:
模式:
查询:
输出:
用户
名称
支付
u_519553787
ccc
F
u_519553787
eee
F