2015-11-06 44 views
2

这个问题是相当模糊,所以我将只举一个例子如何使用where子句对属性的所有实例

如果我有例如DB的国家属性,其中国家可以出现不止有一次,我怎么能检查另一个名为building的属性在该国的所有实例中都是空的?

而且你将如何为所有国家做到这一点?即检查每个国家的所有建筑物对于同一个国家的一组是否为空。

country | building 
     --+-- 
USA  | null 
USA  | null 
USA  | 2 
Germany | null 
Germany | null 
Nepal | null 
Spain | 3 

如果您选择国家,查询应返回德国和尼泊尔。完全随意的例子,但它应该得到的重点。

+0

请编辑你的问题与样本数据和预期的结果。 –

+0

这称为关系部门,如果有帮助的话 –

回答

0

经过在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是表名。

0
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) 
1

这里是另外一个使用GROUP BY:

SELECT country FROM countries group by country having sum(building) is null; 

我在MySQL和SQL Server的测试这一点。