How to use OR Operator in MySQL
Logical Operators are used to test some conditions. If the condition is true, the row will be selected for output.What is OR Logical Operator :
OR logical operator is used when we want to return the row if at least one of the condition is true.Scenario :
Let's say that you have Customer table and you want to return all the rows if FName='Raza' Or CountryShortName='US'.
Create the Customer table by using below script.
Create table Customer
(Id int,
FName VARCHAR(50),
LName VARCHAR(50),
CountryShortName CHAR(2));
insert into Customer
Values (
1,'Raza','M','PK'),
(2,'Rita','John','US'),
(3,'Sukhi','Singh',Null),
(4,'James','Smith','CA'),
(5,'Robert','Ladson','US'),
(6,'Alice','John','US');
We can use below query with OR Logical operator to return all rows where FName='Raza' Or CountryShortName='US'.
Select * From Customer
where FName='Raza'
OR CountryShortName='US';
As we have used OR logical operator in our query, the row will be returned if at-least one condition is true. We can see that for FName='Raza', the row is selected even second condition is not true.Same goes for other three records which are returned even FName is not equal to 'Raza' but CountryShortName is equal to 'US' that makes one condition true.
No comments:
Post a Comment