MySQL 중복 제거

Jmnote (토론 | 기여)님의 2014년 7월 2일 (수) 18:03 판 (→‎같이 보기)

1 개요

MySQL 중복 제거
MySQL 유일키 강제 추가
  • 기존에 중복된 컬럼이 있는 경우 하나만 남기고 다 지우고 싶다.

2 실습

  • id2의 경우 중복된 값이 있는데 하나만 남기고 삭제하고 싶다.
mysql> SELECT * FROM test1;
+----+-------+-----------+
| id | id2   | str       |
+----+-------+-----------+
|  1 | 11111 | hello     |
|  2 | 11111 | world     |
|  3 | 22222 | hello     |
|  4 | 22222 | world     |
|  5 | 11111 | my friend |
+----+-------+-----------+
5 rows in set (0.00 sec)
mysql> ALTER IGNORE TABLE test1 ADD UNIQUE INDEX(id2);
Query OK, 5 rows affected (0.05 sec)
Records: 5  Duplicates: 3  Warnings: 0
mysql> SELECT * FROM test1;
+----+-------+-------+
| id | id2   | str   |
+----+-------+-------+
|  1 | 11111 | hello |
|  3 | 22222 | hello |
+----+-------+-------+
2 rows in set (0.00 sec)

3 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}