论坛首页 综合技术论坛

删除完全相同的记录

浏览 1881 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-05-20  
删除完全相同的记录
如果没有递增字段可以采用临时表技术 
SELECT DISTINCT * INTO #tempTable FROM sourceTable
DELETE FROM sourceTable
INSERT INTO sourceTable SELECT * FROM #tempTable
DROP TABLE #tempTable
 
如果删除的表中存在自动增长列
如:
pid pname
2,"b"
6,"c"
14,"d"
15,"h"
16,"k"
17,"e"
18,"a"
19,"c"
20,"k"
21,"b"
可以采用如下SQL语句
 
DELETE FROM PInfo
WHERE (pid NOT IN
          (SELECT MAX(pid)
         FROM pinfo
         GROUP BY pname))
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics