Having用法 分享
2014-08-18 00:00
183 查看
它主要用于分组后的条件过滤,未分组的用 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;
查询及删除重复记录的方法大全
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(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)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有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)
5、查找表中多余的重复记录(多个字段),不包含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)
相关文章推荐
- Having用法 分享
- 分享:微软提供的一个开源控件treeview 的一小个用法
- iOS开发>学无止境 - UIWebView用法详解及代码分享
- count,group by,having(SQL)的用法
- mysql中having语句与where语句的用法与区别
- SQL中的Where,Group By,Order By和Having的用法/区别
- group by 使用及体会 和having用法
- having用法
- SQL语句HAVING的用法
- IT技术][转]Oracle全套日期用法,与大家分享
- SQL HAVING的用法
- Mysql 基础应用之group by/ having 用法
- Bash Shell中Shift用法分享
- sql中的group by 和 having 用法解析
- 分享若干好多种增多上网站收录的合用法子
- having的用法以及与where区别介绍
- sql group by和having 的用法
- GROUP BY,WHERE,HAVING之间的区别和用法
- 数据库SQL中having和where的用法区别
- order_by_、group_by_、having的用法区别