如何在属性的所有实例上使用 where 子句
How to use a where clause on all instances of an attribute
这个问题比较含糊,我就举个例子吧
如果我有一个数据库,例如带有国家/地区属性,其中国家/地区可以出现不止一次,我如何检查另一个称为建筑物的属性在该国家/地区的所有实例中是否为空?
您将如何为所有国家/地区执行此操作?即检查同一国家/地区的一组所有建筑物对于每个国家/地区是否为空。
country | building
--+--
USA | null
USA | null
USA | 2
Germany | null
Germany | null
Nepal | null
Spain | 3
如果您是 select 国家,则查询应 return 德国和尼泊尔。完全随意的例子,但它应该能说明问题。
已在 Postgres 中签到:select country from countries where country not in (select country from countries where building is not null) group by country order by country;
其中 countries
是 table 名称。
select distinct country
from #table t
where not exists
(select 1 from #table t1 where t1.country = t.country and t1.building is not null)
这是另一个,使用分组依据:
SELECT country FROM countries group by country having sum(building) is null;
我已经在 MySql 和 SQL 服务器上测试过了。
这个问题比较含糊,我就举个例子吧
如果我有一个数据库,例如带有国家/地区属性,其中国家/地区可以出现不止一次,我如何检查另一个称为建筑物的属性在该国家/地区的所有实例中是否为空?
您将如何为所有国家/地区执行此操作?即检查同一国家/地区的一组所有建筑物对于每个国家/地区是否为空。
country | building
--+--
USA | null
USA | null
USA | 2
Germany | null
Germany | null
Nepal | null
Spain | 3
如果您是 select 国家,则查询应 return 德国和尼泊尔。完全随意的例子,但它应该能说明问题。
已在 Postgres 中签到:select country from countries where country not in (select country from countries where building is not null) group by country order by country;
其中 countries
是 table 名称。
select distinct country
from #table t
where not exists
(select 1 from #table t1 where t1.country = t.country and t1.building is not null)
这是另一个,使用分组依据:
SELECT country FROM countries group by country having sum(building) is null;
我已经在 MySql 和 SQL 服务器上测试过了。