CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

点点星灯老师,请看过来!SQL语句的问题请教!

楼主hxg5588(狂学ASP.net)2005-08-02 22:02:39 在 MS-SQL Server / 基础类 提问

看到你回贴,太感谢了!  
  我数据库不是很熟悉。这样把,我写个表啊  
   
  userid       username         text  
  001                 aaa               111  
  001                 aaa               111  
  002                 bbb               222  
  003                 ccc               333  
  003                 ccc               333  
  004                 ddd               444  
  005                 eee               555  
  005                 eee               555  
   
  例如以上这个表啊,001、003、005这几行是重复的吧,我现在要删除掉重复的内容,最后的表如下:  
  userid       username         text  
  001                 aaa               111  
  002                 bbb               222  
  003                 ccc               333  
  004                 ddd               444  
  005                 eee               555  
   
   
  谢谢啊!点点星灯老大:)  
   
  如果哪为老大知道方法,请速告诉小弟!谢谢  
  问题点数:50、回复次数:15Top

1 楼vivianfdlpw()回复于 2005-08-02 22:29:17 得分 0

如果只有这三列:  
  select   distinct   *   from   表  
   
  如果有更多的列:  
  select   *   from   表   t  
  where   not   exists(  
                                      select   1   from   表    
                                      where   userid=t.userid   and  
                                                  username=t.username   and  
                                                  text=t.text   and  
                                                  其他字段>t.其他字段  
                                  )  
  Top

2 楼hxg5588(狂学ASP.net)回复于 2005-08-02 22:37:59 得分 0

楼上的老师,我要删除重复的内容!怎么办?Top

3 楼hxg5588(狂学ASP.net)回复于 2005-08-02 22:40:40 得分 0

我要保证我的每一行数据都是唯一的,没有重复数据。  
  最后的表就是这样的:  
  userid       username         text  
  001                 aaa               111  
  002                 bbb               222  
  003                 ccc               333  
  004                 ddd               444  
  005                 eee               555  
  Top

4 楼filebat(Mark)回复于 2005-08-02 22:49:15 得分 0

--测试数据  
  create   table   ta(userid   varchar(10),   username   varchar(10),   text   varchar(10))  
  insert   ta   select   '001',   'aaa',   '111'   union   all   select   '001',   'aaa',   '111'  
  union   all   select   '002',   'bbb',   '222'   union   all   select   '003',   'ccc',   '333'  
  union   all   select   '003',   'ccc',   '333'   union   all   select   '004',   'ddd',   '444'  
  union   all   select   '005',   'eee',   '555'   union   all   select   '005',   'eee',   '555'  
  --查询  
  declare   @ta   table(userid   varchar(10),   username   varchar(10),   text   varchar(10))  
  insert   @ta   select   distinct   *   from   ta  
  truncate   table   ta  
  insert   ta   select   *   from   @ta  
  select   *   from   ta  
  --清除  
  drop   table   taTop

5 楼filebat(Mark)回复于 2005-08-02 22:50:08 得分 0

不知道能不能不通过中间变量(表变量或临时表)来实现  
  关注..  
  Top

6 楼wangdehao(找找找(现在很幸福))回复于 2005-08-02 23:01:13 得分 0

存在2条完全一样的数据应该是没办法通过一个sql完成吧?  
  filebat(Mark)   的方法应该是最常用最有效的方法了Top

7 楼vivianfdlpw()回复于 2005-08-02 23:05:24 得分 0

--删除重复纪录  
  delete   表    
  from   表   t  
  where   (select   count(1)   from   表   where   userid=t.userid   and   username=t.username   and   [text]=t.[text])>1  
   
  --删除重复纪录并保留一条  
  delete   表    
  from   表   t  
  where   exists(select   1   from   表   where   userid=t.userid   and   username=t.username   and   [text]=t.[text]   and   其他字段>t.其他字段)Top

8 楼vivianfdlpw()回复于 2005-08-02 23:10:17 得分 0

--测试数据  
  declare   @tb   table  
  (  
      ID   int   identity,  
      userid   varchar(10),    
      username   varchar(10),    
      text   varchar(10)  
  )  
  insert   @tb(userid,username,text)  
  select   '001',   'aaa',   '111'   union   all    
  select   '001',   'aaa',   '111'   union   all    
  select   '002',   'bbb',   '222'   union   all    
  select   '003',   'ccc',   '333'   union   all    
  select   '003',   'ccc',   '333'   union   all    
  select   '004',   'ddd',   '444'   union   all    
  select   '005',   'eee',   '555'   union   all    
  select   '005',   'eee',   '555'  
   
  --删除重复纪录  
  delete   @tb    
  from   @tb   t  
  where   (select   count(1)   from   @tb   where   userid=t.userid   and   username=t.username   and   [text]=t.[text])>1  
   
  --查看  
  select   *   from   @tb  
   
  --结果  
  /*  
  ID                     userid           username       text                
  -----------   ----------   ----------   ----------    
  3                       002                 bbb                 222  
  6                       004                 ddd                 444  
   
  (2   row(s)   affected)  
  */  
   
   
  --删除重复纪录并保留一条  
  delete   @tb    
  from   @tb   t  
  where   exists(select   1   from   @tb   where   userid=t.userid   and   username=t.username   and   [text]=t.[text]   and   ID>t.ID)  
  --查看  
  select   *   from   @tb  
   
   
  --结果  
  /*  
  ID                     userid           username       text                
  -----------   ----------   ----------   ----------    
  2                       001                 aaa                 111  
  3                       002                 bbb                 222  
  5                       003                 ccc                 333  
  6                       004                 ddd                 444  
  8                       005                 eee                 555  
   
  (5   row(s)   affected)  
  */  
   
   
  Top

9 楼hxg5588(狂学ASP.net)回复于 2005-08-02 23:39:36 得分 0

哈哈,我找到一个好的办法,不防大家都学学!  
   
   
  在SQL   Server中快速删除重复记录(多图)  
  http://tech.sina.com.cn/s/2004-10-22/1012444979.shtmlTop

10 楼filebat(Mark)回复于 2005-08-02 23:59:17 得分 0

呵呵,    
   
  不知道说什么是好.Top

11 楼xiaonvjing(飞扬)回复于 2005-08-03 05:13:51 得分 0

select   distinct   *   into   #a   from   table1  
  drop   table   table1  
  select   *   into   table1   from   #a  
  drop   table   #a    
  Top

12 楼hglhyy(為人民币服务!)回复于 2005-08-03 08:54:24 得分 0

 
  如果楼主的记录是完全重复,即每个字段都是一样!  
  不防用楼上的试试,最直观!  
   
  begin   tran  
  select   distinct   *   into   #a   from   table1  
  drop   table   table1  
  select   *   into   table1   from   #a  
  drop   table   #a    
  commit   tran  
   
    --select   *   from   table1Top

13 楼aw511(点点星灯)回复于 2005-08-03 19:53:54 得分 0

 
  --创建测试数据  
  CREATE   TABLE   [tttt]   (  
  [userid]   [varchar]   (20)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
  [username]   [varchar]   (20)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
  [text]   [varchar]   (20)   COLLATE   Chinese_PRC_CI_AS   NULL    
  )   ON   [PRIMARY]  
  GO  
   
  insert   tttt   values('001','aaa','111')  
  insert   tttt   values('001','aaa','111')  
  insert   tttt   values('002','bbb','222')  
  insert   tttt   values('003','ccc','333')  
  insert   tttt   values('003','ccc','333')  
  insert   tttt   values('004','ddd','444')  
  insert   tttt   values('005','eee','555')  
  insert   tttt   values('005','eee','555')  
   
  --查看  
  select   *   from   tttt  
   
   
  --要删除重复的数据,加了一个自动增长列  
  select     identity(int,1,1)     as     id,*     into     #temp     from     tttt  
  --select   *   from   #temp  
  delete   from   #temp        
  where     id     not     in          
  (      
        select     max(id)     from     #temp     group     by     userid,username      
  )    
   
  --select   *   from   #temp  
  ---过滤好了所有的数据,开始删除原来的数据,导入新的数据  
  delete   from   tttt  
  insert   tttt  
  select   Userid,username,text   from   #temp  
   
  drop   table   #temp  
  --查看结果  
  select   *   from   tttt  
   
  --删除表  
  drop   table   tttt  
   
   
  ---如果你想知道怎么来的,就自己一步一步的查看Top

14 楼aw511(点点星灯)回复于 2005-08-03 19:55:04 得分 0

不好意思,白天忙.来晚了.Top

15 楼operfume(橘子香水)回复于 2005-08-03 20:13:44 得分 0

使用临时表:  
  select   distinct   *   into   #tmpTable   from   table    
   
  delete   from   table  
   
  insert   into   table   select   *   from   tmpTable  
   
  drop   table   #tmpTable  
  Top

相关问题

  • 求一sql语句!请老师帮忙!急!急!急!
  • 求SQL语句
  • sql语句。
  • sql语句?
  • sql 语句?
  • 求SQL语句
  • ***求SQL语句***
  • sql语句??
  • sql语句
  • sql语句

关键词

  • 字段
  • 数据
  • eee
  • ccc
  • ta
  • userid
  • 重复
  • 删除
  • aaa
  • 表

得分解答快速导航

  • 帖主:hxg5588

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo