萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> sql 刪除重復數據與查找重復數據的方法(1/4)

sql 刪除重復數據與查找重復數據的方法(1/4)

例1,表中有主鍵(可唯一標識的字段),且該字段為數字類型

例1測試數據 /* 表結構 */
drop table if exists `t1`;
create table if not exists `t1`(
  `id` int(1) not null auto_increment,
  `name` varchar(20) not null,
  `add` varchar(20) not null,
  primary key(`id`)
)engine=innodb;

/* 插入測試數據 */
insert into `t1`(`name`,`add`) values
('abc',"123"),
('abc',"123"),
('abc',"321"),
('abc',"123"),
('xzy',"123"),
('xzy',"456"),
('xzy',"456"),
('xzy',"456"),
('xzy',"789"),
('xzy',"987"),
('xzy',"789"),
('ijk',"147"),
('ijk',"147"),
('ijk',"852"),
('opq',"852"),
('opq',"963"),
('opq',"741"),
('tpk',"741"),
('tpk',"963"),
('tpk',"963"),
('wer',"546"),
('wer',"546"),
('once',"546");

select * from `t1`;
+----+------+-----+
| id | name | add |
+----+------+-----+
|  1 | abc  | 123 |
|  2 | abc  | 123 |
|  3 | abc  | 321 |
|  4 | abc  | 123 |
|  5 | xzy  | 123 |
|  6 | xzy  | 456 |
|  7 | xzy  | 456 |
|  8 | xzy  | 456 |
|  9 | xzy  | 789 |
| 10 | xzy  | 987 |
| 11 | xzy  | 789 |
| 12 | ijk  | 147 |
| 13 | ijk  | 147 |
| 14 | ijk  | 852 |
| 15 | opq  | 852 |
| 16 | opq  | 963 |
| 17 | opq  | 741 |
| 18 | tpk  | 741 |
| 19 | tpk  | 963 |
| 20 | tpk  | 963 |
| 21 | wer  | 546 |
| 22 | wer  | 546 |
| 23 | once | 546 |
+----+------+-----+
23 rows in set (0.00 sec) 首頁 1 2 3 4 末頁

copyright © 萬盛學電腦網 all rights reserved