话不多说,请看代码:

if not object_id('Tempdb..#T') is null drop table #TGoCreate table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))Insert #Tselect 1,N'A',N'A1' union allselect 2,N'A',N'A2' union allselect 3,N'A',N'A3' union allselect 4,N'B',N'B1' union allselect 5,N'B',N'B2'Go 

方法1:

delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)

方法2:

delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null

delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)

方法4(注:ID为唯一时可用):

delete a from #T a where ID not in(select min(ID)from #T group by Name)

方法5:

delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0

方法6:

delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)

方法7:

delete a from #T a where ID>any(select ID from #T where Name=a.Name)select * from #T

更多相关文章

  1. SQL Server之JSON 函数详解
  2. MySQL 5.7.9 服务无法启动-“NET HELPMSG 3534”的解决方法
  3. MySQL系列多表连接查询92及99语法示例详解教程
  4. Linux下MYSQL 5.7 找回root密码的问题(亲测可用)
  5. MySQL 什么时候使用INNER JOIN 或 LEFT JOIN
  6. Android(安卓)-- Android(安卓)JUint 与 Sqlite
  7. android中SqLite query中用selectionArgs处理字符传值
  8. android从服务器下载文件(php+apache+win7+MySql)
  9. Android(安卓)ORM SQL Top 5

随机推荐

  1. Android国内开发者陷盈利困局 版本过多支
  2. WebView之js调用Android类的方法传递数据
  3. ExifInterface使用,Android(安卓)2.0新增
  4. Android(安卓)6.0 BluetoothAdapter.star
  5. android中一些view处理(拖动,定位,显示,图层)
  6. [置顶] android Asynctask的优缺点?能否同
  7. ubuntu linux通过adb命令行"复制粘贴"内
  8. Android主流三方库源码分析(一、深入理解O
  9. Nexus One 吃下 Ice Cream Sandwich,操作
  10. 【转】论山寨手机与Android的联姻