MySQL 테이블에서 여러 항목을 삭제하려면 JOIN을 사용하십시오. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTabl(Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100)); Query OK, 0 rows affected (0.69 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(FirstName) values('Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(FirstName) values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.23 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable;
이것은 다음과 같은 출력을 생성합니다 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Bob | | 3 | Mike | | 4 | Adam | | 5 | Bob | | 6 | Adam | | 7 | Bob | | 8 | Mike | +----+-----------+ 8 rows in set (0.00 sec)
다음은 여러 항목을 삭제하는 쿼리입니다 -
mysql> delete DemoTable from DemoTable join DemoTable tbl on DemoTable.FirstName= tbl.FirstName where tbl.FirstName = 'Bob'; Query OK, 3 rows affected (0.15 sec)
테이블 레코드를 다시 한 번 확인합시다 -
mysql> select *from DemoTable;
이것은 다음과 같은 출력을 생성합니다 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 3 | Mike | | 4 | Adam | | 6 | Adam | | 8 | Mike | +----+-----------+ 5 rows in set (0.00 sec)