sql 如何过滤重复记录

如题所述

第1个回答  2016-07-25

SQL过滤重复记录有两种办法:

    通过SQL结构化查询语言来实现,在Select后面加上关键字DISTINCT,意思就是查询行无重复,注意DISTINCT关键字是针对行,不是某一列,如果想得到某一列不重复记录,那就SELECT DISTINCT后面只放一个字段。

    通过存储过程,过滤重复记录,存储过程逐条查询,比对之前的记录,如果有重复就跳到下一条,如果不重复游标继续。

第2个回答  2016-03-29
要求结果1:
如果只取ID列 select distinct ID from table1
如果还有其他列,在id相同情况下,取col1最小的记录。
select ID,col1 from table1 t1 where not exists(select * from table1 where ID=t1.ID and col1>t1.col1)

要求结果2:
select ID from table1 t1 where not exists (select * from table1 where ID=t1.ID)
第3个回答  2016-03-07
有多种方法,
方法一,如前面回答的用:
select distinct tz.id,tz.title from ......
或者group by也行,就是原来的写法在最后加上group by tz.id,tz.title
方法二,用存在性:
select tz.id,tz.title from tz
where exists(select * from cmt where cmt.uid=当前用户的ID and
cmt.tieziID=tz.id
我个人推荐用方法二!
第4个回答  2016-08-24
例如有如下表结构和值
student
fid name sex
1 a 男
2 b 男
3 c 女
4 d 女
5 a 男
6 b 男
distinct去除重复数据
select distinct name from student
select distinct name,id from student
第5个回答  2016-04-23
1.在select的时候使用 distinct
select distinct a from A 这样就从A表里查到不重复的a值记录
2. 使用group
select a from A group by a 也可以按a分组,出来的记录a值也是不重复的
以上是我常用的方法。