一、數(shù)據(jù)庫中的去重操作(刪除數(shù)據(jù)庫中重復記錄的SQL語句)主要有三種方法 (1)、rowid方法 (2)、group by 方法 (3)、distinct方法 1、用rowid方法 根據(jù)Oracle帶的rowid屬性,可以進行判斷是否存在重復語句; (1)、查出表1和表2中name相同的數(shù)據(jù) Select * from table1 a Where rowid !=(select max(rowid) from table2 b Where a.name1 = b.name1 And a.name2 = b.name2......) (2)、刪除表1和表2中name相同的所有數(shù)據(jù) Delete from table1 a Where rowid !=(select max(rowid) From table2 b Where a.name1 = b.name1 And a.name2 = b.name2.......) 2、用group by方法 主要用于分組統(tǒng)計,一般都是使用在聚合函數(shù)中使用; (1)、查數(shù)據(jù) Select count(num), max(name) from student 列出表中的重復的記錄數(shù)和學生名字的屬性, Group by num Having count(num)>1 并按照num分組后找出表中num列出現(xiàn)次數(shù)大于一次的。 (2)、刪除數(shù)據(jù) Delete from student Group by num Having count(num)>1 //刪除表中num列所有重復的數(shù)據(jù) 3、用distinct方法 一般用于比較小的表進行去重,會過濾掉多余的重復記錄,返回不重復的記錄或字段; (1)、select distinct name From student |
|