SQL> desc employee
Name Null? Type
----------------------------------------- -------- ------------------
emp_id NUMBER(10)
emp_name VARCHAR2(20)
salary NUMBER(10,2)
可以通過下面的語句查詢重復的記錄:
SQL> select * from employee;
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine 10000
1 sunshine 10000
2 semon &nbs
2 semon 20000
3 xyz 30000
2 semon 20000
SQL> select distinct * from employee;
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine 10000
2 semon 20000
3 xyz 30000
SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
EMP_ID EMP_NAME
---------- ---------------------------------------- ----------
1 sunshine 10000
2 semon 20000
SQL> select * from employee e1
where
rowid in (select max(rowid) from employe e2e1.emp_name=e2.emp_name and e1.salary=e2.salary);
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine 10000
3 xyz 30000
2 semon 20000
2. 刪除的幾種方法:
(1)通過建立臨時表來實現
SQL>create table temp_emp as (select distinct * from employee)
SQL> truncate table employee; (清空employee表的數據)
SQL> insert into employee select * from temp_emp; (再將臨時表裡的內容插回來)
( 2)通過唯一rowid實現刪除重復記錄.在Oracle中,
每一條記錄都有一個rowid,rowid在整個數據庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個數據文件、塊、行上。在重復的記錄中,可能所有列的內容都相同,但rowid不會相同,所以只要確定出重復記錄中那些具有最大或最小rowid的就可以了,其余全部刪除。
SQL>delete from employee e2 where rowid not in (
select max(e1.rowid) from employee e1 where
e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--這裡用min(rowid)也可以。
SQL>delete from employee e2 where rowid <(
select max(e1.rowid) from employee e1 where
e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and
(3)也是通過rowid,但效率更高。
SQL>delete from employee where rowid not in (
select max(t1.rowid) from employee t1 group by
t1.emp_id,t1.emp_name,t1.salary);--這裡用min(rowid)也可以。
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine 10000
3 xyz 30000
2 semon 20000
SQL> desc employee
Name &nb
emp_id NUMBER(10)
emp_name VARCHAR2(20)
salary NUMBER(10,2)
可以通過下面的語句查詢重復的記錄:
SQL> select * from employee;
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine 10000
1 sunshine 10000
2 semon 20000
2 semon &nb
您正在看的SQLserver教程是:在SQL刪除重復記錄(多種方法)。sp; 20000
3 xyz 30000
2 semon 20000 SQL> select distinct * from employee;
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine 10000
2 semon 20000
3 xyz 30000
SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine &
您正在看的SQLserver教程是:在SQL刪除重復記錄(多種方法)。nbsp; 10000
2 semon 20000
SQL> select * from employee e1
where rowid in (select max(rowid) from employe e2
where e1.emp_id=e2.emp_id and
e1.emp_name=e2.emp_name and e1.salary=e2.salary);
EMP_ID EMP_NAME SALARY
---------- ---------------------------------------- ----------
1 sunshine 10000
3 xyz 30000
2 semon 20000
本新聞共2頁,當前在第1頁 1 2