过滤重复记录
来源:互联网 发布:nginx last break 编辑:程序博客网 时间:2024/06/05 10:32
create table tb(学校 varchar(10),年级 int,班级 varchar(10),姓名 varchar(10),性别 varchar(10),年龄 int)
insert into tb values('1中', 1, '2班', '张', '男', 20)
insert into tb values('2中', 3, '1班', '马', '男', 17)
insert into tb values('1中', 2, '2班', '李', '女', 16)
insert into tb values('2中', 3, '5班', '张', '男', 15)
insert into tb values('2中', 3, '6班', '李', '女', 18)
go
--方法1:
select a.* from tb a where 年龄 = (select min(年龄) from tb where 姓名 = a.姓名 and 性别 = a.性别) order by a.学校
--方法2:
select a.* from tb a where not exists(select 1 from tb where 姓名 = a.姓名 and 性别 = a.性别 and 年龄 < a.年龄) order by a.学校
--方法3:
select a.* from tb a,(select 姓名,性别,min(年龄) 年龄 from tb group by 姓名,性别) b where a.姓名 = b.姓名 and a.性别 = b.性别 and a.年龄 = b.年龄 order by a.学校
--方法4:
select a.* from tb a inner join (select 姓名,性别 , min(年龄) 年龄 from tb group by 姓名,性别) b on a.姓名 = b.姓名 and a.性别 = b.性别 and a.年龄 = b.年龄 order by a.学校
--方法5
select a.* from tb a where 1 > (select count(*) from tb where 姓名 = a.姓名 and 性别 = a.性别 and 年龄 < a.年龄) order by a.学校
drop table tb
/*
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
*/
insert into tb values('1中', 1, '2班', '张', '男', 20)
insert into tb values('2中', 3, '1班', '马', '男', 17)
insert into tb values('1中', 2, '2班', '李', '女', 16)
insert into tb values('2中', 3, '5班', '张', '男', 15)
insert into tb values('2中', 3, '6班', '李', '女', 18)
go
--方法1:
select a.* from tb a where 年龄 = (select min(年龄) from tb where 姓名 = a.姓名 and 性别 = a.性别) order by a.学校
--方法2:
select a.* from tb a where not exists(select 1 from tb where 姓名 = a.姓名 and 性别 = a.性别 and 年龄 < a.年龄) order by a.学校
--方法3:
select a.* from tb a,(select 姓名,性别,min(年龄) 年龄 from tb group by 姓名,性别) b where a.姓名 = b.姓名 and a.性别 = b.性别 and a.年龄 = b.年龄 order by a.学校
--方法4:
select a.* from tb a inner join (select 姓名,性别 , min(年龄) 年龄 from tb group by 姓名,性别) b on a.姓名 = b.姓名 and a.性别 = b.性别 and a.年龄 = b.年龄 order by a.学校
--方法5
select a.* from tb a where 1 > (select count(*) from tb where 姓名 = a.姓名 and 性别 = a.性别 and 年龄 < a.年龄) order by a.学校
drop table tb
/*
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
学校 年级 班级 姓名 性别 年龄
---------- ----------- ---------- ---------- ---------- -----------
1中 2 2班 李 女 16
2中 3 1班 马 男 17
2中 3 5班 张 男 15
(所影响的行数为 3 行)
*/
- 过滤重复记录
- SQL重复记录过滤
- SQL重复记录处理(查找,过滤,删除)
- SQL重复记录处理(查找,过滤,删除)
- SQL重复记录处理(查找,过滤,删除)
- 过滤掉内表中没有重复记录的数据
- SQL重复记录处理(查找,过滤,删除)
- SQL重复记录处理(查找,过滤,删除)
- SQL重复记录处理(查找,过滤,删除)
- SQL重复记录处理(查找,过滤,删除)
- Oracle 中按条件过滤重复记录
- 过滤dt中重复的记录
- 数据库中 join 链接过滤重复的记录
- 解决sql 过滤重复数据记录的方法(Oracle)
- SQL过滤、删除、表中重复记录语句
- mysql查询过滤重复数据并随机取出记录
- 过滤重复
- Oracle:行转列函数,查询重复记录函数,过滤表中重复记录函数
- 学习linux/unix编程方法的建议
- OCCI开发中getString()方法使用中的问题
- Windows Mobile 下显示 Flash Card 容量和序列号的代码[转载]
- vb.net中自定义属性的应用
- Outlook 命令行
- 过滤重复记录
- proxool用法
- 微软的面试题及答案-超变态但是很经典
- 问题解决:SQLException: org.apache.tomcat.dbcp.dbcp.DelegatingStatement with address: "com.mysql.jdbc.Statement@100363" is closed.
- powerbuilder的dw中使用graph风格,当横轴是日期时,如何显示才能完整显示日期?
- MFC 对话 模式和非模式显示
- DWR之入门学习笔记
- GWT简单应用-RPC篇(奥运奖牌版小偷)
- 用VC纯资源dll制作多语言界面程序