Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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中,如果我有此类记录: |---------------------|------------------| | Name | Paid | |---------------------|------------------| | Teddy | No | |---------------------|------------------| | And

在PostgreSQL中,如果我有此类记录:

|---------------------|------------------|
|      Name           |     Paid         |
|---------------------|------------------|
|        Teddy        |        No        |
|---------------------|------------------|
|        Andy         |        No        |
|---------------------|------------------|
|        Andy         |        Yes       |
|---------------------|------------------|
|        Sarah        |        Yes       |
|---------------------|------------------|
|        Michael      |        No        |
|---------------------|------------------|
|        Michael      |        Yes       |
|---------------------|------------------|
如何获取表中未出现“是”的名称

在这个例子中,结果应该是:Teddy

谢谢

选择名称 从桌子上 名字不在哪里 选择名称 从桌子上 where paid='是' 我将不存在与相关子查询一起使用:

select t.name, t.paid
from mytable t
where not exists (
    select 1 from mytable t1 where t1.name = t.name and t1.paid = 'Yes'
)

对于此查询的性能,您需要一个名为paid的索引。

假设值仅为“Yes”和“No”:

另一种选择:

select name 
from yourtable
group by name
having count (*) filter (where paid = 'Yes') = 0
这最多只能对表进行一次扫描,如果表很大,这可能会有所帮助

select name 
from yourtable
group by name
having count (*) filter (where paid = 'Yes') = 0