Delete selective multiple records using MySQL DELETE query


For selective multiple records, use MySQL IN(). To delete them, use MySQL DELETE. Let us first create a table −

mysql> create table DemoTable
(
   ClientId varchar(40),
   ClientName varchar(50)
);
Query OK, 0 rows affected (0.62 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('CLI-101','Chris');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values('CLI-110','Adam');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('CLI-220','Mike');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('CLI-120','Bob');
Query OK, 1 row affected (0.53 sec)
mysql> insert into DemoTable values('CLI-240','Sam');
Query OK, 1 row affected (0.06 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+------------+
| ClientId | ClientName |
+----------+------------+
| CLI-101  | Chris      |
| CLI-110  | Adam       |
| CLI-220  | Mike       |
| CLI-120  | Bob        |   
| CLI-240  | Sam        |
+----------+------------+
5 rows in set (0.00 sec)

Here is the query to delete selective multiple records −

mysql> delete from DemoTable where ClientId IN('CLI-101','CLI-220','CLI-240');
Query OK, 3 rows affected (0.10 sec)

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+----------+------------+
| ClientId | ClientName |
+----------+------------+
| CLI-110  | Adam       |
| CLI-120  | Bob        |
+----------+------------+
2 rows in set (0.00 sec)
raja
Published on 09-Oct-2019 14:12:31
Advertisements