Leakbali
w3 Tutorial, Web Tutorial
Switch to English Bahasa Indonesia 
Register   Login

MySQL - And & Or


The AND & OR operators are used to filter records based on more than one condition. The AND operator displays a record if both the first condition and the second condition is true. The OR operator displays a record if either the first condition or the second condition is true.

AND Operator Example

The "Persons" Table
IDFirst NameLast NameAddress
1PutuSanjayaDenpasar
2MadeSanjayaGianyar
3HaruSanjayaSanur
Now we want to select only the persons with the first name equal to "Haru" AND the last name equal to "Sanjaya". We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Haru'
AND LastName='Sanjaya'
The result-set will look like this:
IDFirst NameLast NameAddress
3HaruSanjayaSanur

OR Operator Example

Now we want to select only the persons with the first name equal to "Putu" OR the first name equal to "Haru". We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Putu'
OR FirstName='Haru'
The result-set will look like this:
IDFirst NameLast NameAddress
1PutuSanjayaDenpasar
3HaruSanjayaSanur

Combining AND & OR

You can also combine AND and OR (use parenthesis to form complex expressions). Now we want to select only the persons with the last name equal to "Sanjaya" AND the first name equal to "Putu" OR to "Haru". We use the following SELECT statement:
SELECT * FROM Persons WHERE
LastName='Sanjaya'
AND (FirstName='Putu' OR FirstName='Haru')
The result-set will look like this:
IDFirst NameLast NameAddress
1PutuSanjayaDenpasar
3HaruSanjayaSanur

References

About Us

Home
About Us
Contact Us
Sitemap

Tools

Google PageRank
Alexa Rank
Keywords Density

Accounts

Register Account
Login
Valid XHTML 1.0 TransitionalValid CSS!
Web Directory


2006 - 2012 © Leakbali.com - Free Web Tutorial, Free Web Articles, Web Sharing, Source Codes, Web References