How to use three conditions in a single MySQL query with id, name and age of students to fetch record of a student?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(50),
   StudentAge int
);
Query OK, 0 rows affected (0.72 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(StudentName,StudentAge) values('Chris',21);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('David',23);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('Bob',22);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('Carol',21);
Query OK, 1 row affected (0.30 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output &minusl;

+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
|         1 | Chris       |         21 |
|         2 | David       |         23 |
|         3 | Bob         |         22 |
|         4 | Carol       |         21 |
+-----------+-------------+------------+
4 rows in set (0.00 sec)

Following is the query to implement three conditions to fetch a particular record −

mysql> select *from DemoTable where StudentId=4 and StudentName='Carol' and StudentAge=21;

This will produce the following output −

+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
|         4 | Carol       |         21 |
+-----------+-------------+------------+
1 row in set (0.00 sec)
raja
Published on 03-Oct-2019 06:05:44
Advertisements