PostgreSQL简单查询

PostgreSQL简单查询,sql,postgresql,Sql,Postgresql,我的问题是编写一个查询(PostgreSQL)来获取一个'propno',其中'newsname'不等于'NYT'(答案是40) 感谢您的帮助您可以使用聚合和拥有: select propno from t group by propno having count(*) filter (where newsname = 'NYT') = 0; 如果您有一个单独的表,每个propno有一行,那么我建议不存在: select p.propno from props p where not exis

我的问题是编写一个查询(PostgreSQL)来获取一个'propno',其中'newsname'不等于'NYT'(答案是40)


感谢您的帮助

您可以使用聚合和
拥有

select propno
from t
group by propno
having count(*) filter (where newsname = 'NYT') = 0;
如果您有一个单独的表,每个
propno
有一行,那么我建议
不存在

select p.propno
from props p
where not exists (select 1
                  from t
                  where t.propno = p.propno and
                        t.newname = 'NYT'
                 );
您可以使用exists:

select propno
from props p
where not exists (
  select 1
  from props t
  where t.propno = p.propno
  and t.newname = 'NYT'
);

具有
可用于过滤掉
'NYT'

SELECT propno
FROM tab_name
GROUP BY propno
HAVING SUM(newsname='NYT')=0;

你想要最大限度的支持吗?否则,结果是40、10和30。如果这是针对postgres的,为什么要添加sqlite和mariadb标记?不,你看到NYT已经有了10和30。我需要查询哪个会打印40,因为“纽约时报”没有40。
SELECT propno
FROM tab_name
GROUP BY propno
HAVING SUM(newsname='NYT')=0;