MySQL to Implementing OR operator in a WHERE clause?


The OR operator gives true result when any one operand is true. Let us now see an example and create a table −

mysql> create table DemoTable663(ClientId int,ClientName varchar(100),ClientAge int);
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable663 values(100,'Chris',45);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable663 values(101,'Robert',29);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable663 values(102,'John',45);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable663 values(103,'Chris',35);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable663 values(104,'Sam',45);
Query OK, 1 row affected (0.72 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable663;

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
| 100      | Chris      |        45 |
| 101      | Robert     |        29 |
| 102      | John       |        45 |
| 103      | Chris      |        35 |
| 104      | Sam        |        45 |
+----------+------------+-----------+
5 rows in set (0.00 sec)

Following is the query to implement OR operator −

mysql> select *from DemoTable663 where ClientName='Chris' OR ClientAge=45;

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
| 100      | Chris      |        45 |
| 102      | John       |        45 |
| 103      | Chris      |        35 |
| 104      | Sam        |        45 |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Updated on: 23-Aug-2019

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements