MySQL query to display the column and its values using OR in WHERE statement


Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable777(StudentName,StudentAge) values('Chris',23);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Robert',21);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Mike',19);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Bob',22);
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable777;

This will produce the following output -

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

Following is the query to return all the columns using OR in WHERE statement −

mysql> select *from DemoTable777 where StudentId IN(1,2) OR StudentName='Mike' OR StudentAge=22;

This will produce the following output -

+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
| 1         | Chris       | 23         |
| 2         | Robert      | 21         |
| 3         | Mike        | 19         |
| 4         | Bob         | 22         |
+-----------+-------------+------------+
4 rows in set (0.00 sec)
raja
Published on 09-Sep-2019 11:32:13
Advertisements