Delete a specific record from a MySQL table by using AND in WHERE clause

MySQLMySQLi Database

MySQL AND is used in WHERE to fetch a record by filtering using multiple conditions. Let us first create a table−

mysql> create table DemoTable
   -> (
   -> Id int,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(101,'Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(102,'David');
Query OK, 1 row affected (0.51 sec)
mysql> insert into DemoTable values(103,'Bob');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+------+-------+
| Id   | Name  |
+------+-------+
| 101  | Chris |
| 102  | David |
| 103  | Bob   |
+------+-------+
3 rows in set (0.00 sec)

Here is the query to delete a record −

mysql> delete from DemoTable where Id=102 and Name='David';
Query OK, 1 row affected (0.20 sec)

Let us check the table records once again −

mysql> select * from DemoTable;

This will produce the following output −

+------+-------+
| Id   | Name  |
+------+-------+
| 101  | Chris |
| 103  | Bob   |
+------+-------+
2 rows in set (0.00 sec)
raja
Published on 17-Dec-2019 10:50:13
Advertisements