Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql排除查询_Sql_Postgresql - Fatal编程技术网

Postgresql排除查询

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

一般来说,我对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
                  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