在postgresql

时间:2015-08-12 06:32:39

标签: sql postgresql

我在postgresql中有表区,其中包含Column和tehsil列。 Tehsil名称不是唯一的,同名的tehsil属于多个区域。我想找出同名的tehsils属于不同的地区..

Tehsil  District

Alot    Ratlam
Alur    Hassan
Alur    Kurnool
Aluva   Ernakulam
Khanapur    Adilabad
Khanapur    Belgaum
Khanapur    Sangli
Khandaghosh Barddhaman
Khandagiri  Khordha

2 个答案:

答案 0 :(得分:1)

试试这个

select Tehsil from table
group by Tehsil 
having min(District)<>max(District)

了解他们所属的地区

select t1.Tehsil, t1.district from table as t1 inner join 
(
select Tehsil from table
group by Tehsil 
having min(District)<>max(District)
) as t2 on t1.Tehsil =t2.Tehsil 

答案 1 :(得分:1)

试试这种方式

select Tehsil from table
group by Tehsil 
having count(Tehsil)>1