Having用法 分享

它主要用於分組後的條件過濾,未分組的用 where,若是用了group by 那麼後面的過濾條件就不能再用where了,要用having
Select COUNT(PartID) ,PartCode,PartCode2 From Base_Part   Group by PartCode,PartCode2  having count(PartID) >1
 
2.group by方法
查數據:
  select count(num), max(name) from student --列出重複的記錄數,並列出他的name屬性
  group by num
  having count(num) >1 --按num分組後找出表中num列重複,即出現次數大於一次
刪數據:
  delete from student
  group by num
  having count(num) >1
  這樣的話就把全部重複的都刪除了。
3.用distinct方法 -對於小的表比較有用
create table table_new as   select distinct *   from table1 minux
truncate table table1;
insert into table1 select * from table_new;

查詢及刪除重複記錄的方法大全 一、查找表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷select * from people where peopleId in (select  peopleId  from  people  group  by  peopleId  having  count(peopleId) > 1) 二、刪除表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷,只留有rowid最小的記錄 delete from people where peopleId  in (select  peopleId  from people  group  by  peopleId   having  count(peopleId) > 1) and rowid not in (select min(rowid) from  people  group by peopleId  having count(peopleId )>1) 三、查找表中多餘的重複記錄(多個字段) select * from vitae a where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq  having count(*) > 1) 四、刪除表中多餘的重複記錄(多個字段),只留有rowid最小的記錄 delete from vitae a where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) 五、查找表中多餘的重複記錄(多個字段),不包含rowid最小的記錄 select * from vitae a where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
相關文章
相關標籤/搜索