SQL不重复记录

由于设计需要,需要查询不重复的记录值,同类问题,想必大家都遇到过,于是乎马上GOOGLE一下,发现此类问题还挺多,解决方案也不少,仔细看看。
例如有如下表结构和值
table
fid   name  sex
       
       
       
       
       
       
方案一:distinct
select distinct name from table
得到结果:
name
a
b
c
d
实现效果,那如果要同时打开其它记录呢?再试试
select distinct name,id from table
测试没什么效果,查下得知,这样实际是要name和id字段都重复才被筛选。继续查找可得如下方法:
方案二:group by
select *, count(distinct name) from table group by name

Oracle下测试失败,据说MYSQL下通过,不管,继续思考....
翻翻书,试试
select min(fid),name,sex from table group by name
成功,现实如下结果:
fid   name  sex
       
       
       
       
继续思考,如果要打开所有记录,不指定字段用(*),貌似这方法就不行了!
select  * from table where fid in(Select min(fid) FROM table group by name)
测试成功
fid   name  sex
       
       
       
       
方案三:
本来已经完了,突然想起前几天在网上查了查询数据中所有某字段不重复的记录
select    from   table   where   name in(select   name   from   table   group   by   name     having   count(name)=2)
得到如下结果:
fid   name  sex
       
       
       
       
以此类推:
select    from   table   where   name in(select   name   from   table   group   by   name     having   count(name)=1)
按道理说没问题,大家试试~~
再多的字段都全部现实。哎,原来如此简单!回顾网上方法distinct,Inner   Join等等,麻烦,而且有很大局限性.
总结如下:
select distinct name from table打开不重复记录的单个字段
select  * from table where fid in(Select min(fid) FROM table group by name)打开不重复记录的所有字段值
select    from   table   where   name in(select   name   from   table   group   by   name     having   count(name)=1)打开不重复任意次数的所有记录

----------------------------------------------------------------------------------------------

--生成测试数据  
  create   table   test(id   int,product   varchar(10),price   int,[datetime]   datetime)  
  insert   into   test   select   1,'apple'   ,11,'2005-02-02'  
  insert   into   test   select   2,'apple'   ,22,'2005-02-06'  
  insert   into   test   select   3,'apple'   ,33,'2005-05-05'  
  insert   into   test   select   4,'orange',44,'2005-02-03'  
  insert   into   test   select   5,'orange',55,'2005-09-09'  
  insert   into   test   select   6,'aa'         ,66,'2005-09-01'  
  insert   into   test   select   7,'bb'         ,77,'2005-11-09'  
   
   
  --查询方式一  
  select  
          a.*  
  from  
          test   a  
  where  
          a.id   =   (select   top   1   id   from   test   where   product=a.product   order   by   [datetime]   desc)  
  order   by  
          a.id  
           
  --输出结果  
  /*  
  id       product     price     datetime  
  ---     -------     -----     -----------------------  
  3         apple         33           2005-05-05   00:00:00.000  
  5         orange       55           2005-09-09   00:00:00.000  
  6         aa               66           2005-09-01   00:00:00.000  
  7         bb               77           2005-11-09   00:00:00.000*/  
   
   
  --查询方式二  
  select  
          a.*  
  from  
          test   a  
  where  
          a.id   =   (select   max(id)   from   test   where   product=a.product)  
  order   by  
          a.id  
   
  --输出结果  
  /*  
  id       product     price     datetime  
  ---     -------     -----     -----------------------  
  3         apple         33           2005-05-05   00:00:00.000  
  5         orange       55           2005-09-09   00:00:00.000  
  6         aa               66           2005-09-01   00:00:00.000  
  7         bb               77           2005-11-09   00:00:00.000*/  
   
   
  --查询方式三  
  select  
          a.*  
  from  
          test   a  
  where  
          not   exists(select   1   from   test   where   product=a.product   and   [datetime]>a.[datetime])  
  order   by  
          a.id  
   
  --输出结果  
  /*  
  id       product     price     datetime  
  ---     -------     -----     -----------------------  
  3         apple         33           2005-05-05   00:00:00.000  
  5         orange       55           2005-09-09   00:00:00.000  
  6         aa               66           2005-09-01   00:00:00.000  
  7         bb               77           2005-11-09   00:00:00.000*/   
      
  --删除测试数据  
  drop   table   test

原文地址:https://www.cnblogs.com/chuncn/p/1427969.html