ms sql方法:
declare @sss table (ID int, Name varchar(10), address varchar(10))
insert into @sss select 1,'小王','重庆'
insert into @sss select 2,'小张','北京'
insert into @sss select 3,'小明','上海'
insert into @sss select 4,'小红','重庆'
insert into @sss select 5,'小李','重庆'
insert into @sss select 6,'小白','北京'
insert into @sss select 7,'小红','上海'
insert into @sss select 8,'小红','重庆'
delete from @sss where id not in(select min(id) from @sss group by name,address)
------------------------------------------------------------------------------------------------------------
oracle方法:
SQL> CREATE TABLE test_delete(
2 name varchar(10),
3 value INT
4 );
表已创建。
SQL> INSERT INTO test_delete
2 SELECT '张三', 100 FROM dual
3 UNION ALL SELECT '张三', 100 FROM dual
4 UNION ALL SELECT '李四', 80 FROM dual
5 UNION ALL SELECT '王五', 80 FROM dual
6 UNION ALL SELECT '王五', 80 FROM dual
7 UNION ALL SELECT '赵六', 90 FROM dual
8 UNION ALL SELECT '赵六', 70 FROM dual;
已创建7行。
SQL> SELECT
2 ROWID,
3 name,
4 value
5 FROM
6 test_delete;
ROWID NAME VALUE
------------------ ---------- ----------
AAAM2mAAGAAAAOXAAA 张三 100
AAAM2mAAGAAAAOXAAB 张三 100
AAAM2mAAGAAAAOXAAC 李四 80
AAAM2mAAGAAAAOXAAD 王五 80
AAAM2mAAGAAAAOXAAE 王五 80
AAAM2mAAGAAAAOXAAF 赵六 90
AAAM2mAAGAAAAOXAAG 赵六 70
已选择7行。
SQL> DELETE test_delete
WHERE (name, value) IN (SELECT name, value FROM test_delete GROUP BY name, value HAVING COUNT(1) > 1)
AND rowid NOT IN (SELECT MIN(rowid) FROM test_delete GROUP BY name, value HAVING COUNT(1) > 1);
已删除2行。
SQL> SELECT
2 ROWID,
3 name,
4 value
5 FROM
6 test_delete;
ROWID NAME VALUE
------------------ ---------- ----------
AAAM2mAAGAAAAOXAAA 张三 100
AAAM2mAAGAAAAOXAAC 李四 80
AAAM2mAAGAAAAOXAAD 王五 80
AAAM2mAAGAAAAOXAAF 赵六 90
AAAM2mAAGAAAAOXAAG 赵六 70