먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100) ); Query OK, 0 rows affected (0.46 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.09 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable;
이것은 다음과 같은 출력을 생성합니다 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Robert | | 3 | Robert | | 4 | John | | 5 | John | | 6 | Mike | +----+-----------+ 6 rows in set (0.00 sec)
다음은 MySQL 테이블에서 중복 값을 제거하는 쿼리입니다 -
mysql> delete tbl from DemoTable tbl left join( select min(Id) as Id, FirstName from DemoTable group by FirstName ) tbl1 ON tbl.Id = tbl1.Id AND tbl.FirstName = tbl1.FirstName where tbl1.Id IS NULL; Query OK, 2 rows affected (0.16 sec)
다시 한번 테이블 기록을 확인해보자.
mysql> select *from DemoTable;
이것은 다음과 같은 출력을 생성합니다 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Robert | | 4 | John | | 6 | Mike | +----+-----------+ 4 rows in set (0.00 sec)